Skip to content

Commit 36588c4

Browse files
committed
api: Use exponential backoff in call_on_each_event.
Previously we paused 1s after each failure.
1 parent 04e43b9 commit 36588c4

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
@@ -628,7 +628,13 @@ def do_register():
628628
# Make long-polling requests with `get_events`. Once a request
629629
# has received an answer, pass it to the callback and before
630630
# making a new long-polling request.
631-
while True:
631+
# NOTE: Back off exponentially to cover against potential bugs in this
632+
# library causing a DoS attack against a server when getting errors
633+
# (explicit values listed for clarity)
634+
backoff = RandomExponentialBackoff(maximum_retries=10,
635+
timeout_success_equivalent=300,
636+
delay_cap=90)
637+
while backoff.keep_going():
632638
if queue_id is None:
633639
(queue_id, last_event_id) = do_register()
634640

@@ -658,12 +664,11 @@ def do_register():
658664
#
659665
# Reset queue_id to register a new event queue.
660666
queue_id = None
661-
# Add a pause here to cover against potential bugs in this library
662-
# causing a DoS attack against a server when getting errors.
663-
# TODO: Make this back off exponentially.
664-
time.sleep(1)
667+
668+
backoff.fail()
665669
continue
666670

671+
backoff.succeed()
667672
for event in res['events']:
668673
last_event_id = max(last_event_id, int(event['id']))
669674
callback(event)

0 commit comments

Comments
 (0)