Skip to content

Commit dc4404f

Browse files
committed
api: Inline nested do_register function in call_on_each_event.
1 parent 292d320 commit dc4404f

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

zulip/zulip/__init__.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -601,8 +601,16 @@ def call_on_each_event(
601601
if narrow is None:
602602
narrow = []
603603

604-
def do_register(backoff: RandomExponentialBackoff) -> Tuple[str, int]:
605-
while backoff.keep_going():
604+
queue_id = None
605+
# NOTE: Back off exponentially to cover against potential bugs in this
606+
# library causing a DoS attack against a server when getting errors
607+
# (explicit values listed for clarity)
608+
backoff = RandomExponentialBackoff(maximum_retries=10,
609+
timeout_success_equivalent=300,
610+
delay_cap=90)
611+
while backoff.keep_going():
612+
# Ensure event queue exists (or continues to do so)
613+
if queue_id is None:
606614
if event_types is None:
607615
res = self.register()
608616
else:
@@ -612,24 +620,14 @@ def do_register(backoff: RandomExponentialBackoff) -> Tuple[str, int]:
612620
if self.verbose:
613621
print("Server returned error:\n%s" % res['msg'])
614622
backoff.fail()
623+
continue
615624
else:
616625
backoff.succeed()
617-
return (res['queue_id'], res['last_event_id'])
618-
619-
queue_id = None
620-
# Make long-polling requests with `get_events`. Once a request
621-
# has received an answer, pass it to the callback and before
622-
# making a new long-polling request.
623-
# NOTE: Back off exponentially to cover against potential bugs in this
624-
# library causing a DoS attack against a server when getting errors
625-
# (explicit values listed for clarity)
626-
backoff = RandomExponentialBackoff(maximum_retries=10,
627-
timeout_success_equivalent=300,
628-
delay_cap=90)
629-
while backoff.keep_going():
630-
if queue_id is None:
631-
(queue_id, last_event_id) = do_register(backoff)
626+
queue_id, last_event_id = res['queue_id'], res['last_event_id']
632627

628+
# Make long-polling requests with `get_events`. Once a request
629+
# has received an answer, pass it to the callback and before
630+
# making a new long-polling request.
633631
res = self.get_events(queue_id=queue_id, last_event_id=last_event_id)
634632
if 'error' in res['result']:
635633
if res["result"] == "http-error":

0 commit comments

Comments
 (0)