Skip to content

Commit 1c88115

Browse files
committed
api: Use exponential backoff in call_on_each_event.
Previously we paused 1s after each failure.
1 parent 1dac75f commit 1c88115

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

zulip/zulip/__init__.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,13 @@ def do_register() -> Tuple[str, int]:
619619
# Make long-polling requests with `get_events`. Once a request
620620
# has received an answer, pass it to the callback and before
621621
# making a new long-polling request.
622-
while True:
622+
# NOTE: Back off exponentially to cover against potential bugs in this
623+
# library causing a DoS attack against a server when getting errors
624+
# (explicit values listed for clarity)
625+
backoff = RandomExponentialBackoff(maximum_retries=10,
626+
timeout_success_equivalent=300,
627+
delay_cap=90)
628+
while backoff.keep_going():
623629
if queue_id is None:
624630
(queue_id, last_event_id) = do_register()
625631

@@ -649,12 +655,11 @@ def do_register() -> Tuple[str, int]:
649655
#
650656
# Reset queue_id to register a new event queue.
651657
queue_id = None
652-
# Add a pause here to cover against potential bugs in this library
653-
# causing a DoS attack against a server when getting errors.
654-
# TODO: Make this back off exponentially.
655-
time.sleep(1)
658+
659+
backoff.fail()
656660
continue
657661

662+
backoff.succeed()
658663
for event in res['events']:
659664
last_event_id = max(last_event_id, int(event['id']))
660665
callback(event)

0 commit comments

Comments
 (0)