Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2839d0b

Browse files
committed
Add re-open throttling to ResumableBidiRpc
1 parent 1a1f08f commit 2839d0b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

api_core/google/api_core/bidi.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ def __init__(self, start_rpc, should_recover, initial_request=None, metadata=Non
397397
self._operational_lock = threading.RLock()
398398
self._finalized = False
399399
self._finalize_lock = threading.Lock()
400+
self._reopen_throttle = _Throttle(time_window=10, entry_cap=5)
400401

401402
def _finalize(self, result):
402403
with self._finalize_lock:
@@ -440,7 +441,8 @@ def _reopen(self):
440441
# retryable error.
441442

442443
try:
443-
self.open()
444+
with self._reopen_throttle:
445+
self.open()
444446
# If re-opening or re-calling the method fails for any reason,
445447
# consider it a terminal error and finalize the stream.
446448
except Exception as exc:
@@ -639,7 +641,7 @@ def start(self):
639641
thread = threading.Thread(
640642
name=_BIDIRECTIONAL_CONSUMER_NAME,
641643
target=self._thread_main,
642-
args=(ready,)
644+
args=(ready,),
643645
)
644646
thread.daemon = True
645647
thread.start()

api_core/tests/unit/test_bidi.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,20 @@ def test_reopen_failure_on_rpc_restart(self):
513513
assert bidi_rpc.is_active is False
514514
callback.assert_called_once_with(error2)
515515

516+
def test_throttles_reopen_requests(self):
517+
call = CallStub([])
518+
start_rpc = mock.create_autospec(
519+
grpc.StreamStreamMultiCallable, instance=True, return_value=call
520+
)
521+
should_recover = mock.Mock(spec=["__call__"], return_value=True)
522+
bidi_rpc = bidi.ResumableBidiRpc(start_rpc, should_recover)
523+
524+
patcher = mock.patch.object(bidi_rpc._reopen_throttle.__class__, "__enter__")
525+
with patcher as mock_enter:
526+
bidi_rpc._reopen()
527+
528+
mock_enter.assert_called_once()
529+
516530
def test_send_not_open(self):
517531
bidi_rpc = bidi.ResumableBidiRpc(None, lambda _: False)
518532

0 commit comments

Comments
 (0)