Skip to content

Commit

Permalink
Fix bad_sync_timeout to be a reasonable starting value (#184)
Browse files Browse the repository at this point in the history
Also make bad_sync_timeout into a kwarg just in case.

Signed-off-by: Adam Beckmeyer <adam_gpg@thebeckmeyers.xyz>
  • Loading branch information
non-Jedi authored Mar 26, 2018
1 parent b544a46 commit 06ba97e
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions matrix_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ def listen_for_events(self, timeout_ms=30000):
"""
self._sync(timeout_ms)

def listen_forever(self, timeout_ms=30000, exception_handler=None):
def listen_forever(self, timeout_ms=30000, exception_handler=None,
bad_sync_timeout=5):
""" Keep listening for events forever.
Args:
Expand All @@ -420,21 +421,23 @@ def listen_forever(self, timeout_ms=30000, exception_handler=None):
exception_handler (func(exception)): Optional exception handler
function which can be used to handle exceptions in the caller
thread.
aad_sync_timeout (int): Base time to wait after an error before
retrying. Will be increased according to exponential backoff.
"""
bad_sync_timeout = 5000
_bad_sync_timeout = bad_sync_timeout
self.should_listen = True
while (self.should_listen):
try:
self._sync(timeout_ms)
bad_sync_timeout = 5
_bad_sync_timeout = bad_sync_timeout
except MatrixRequestError as e:
logger.warning("A MatrixRequestError occured during sync.")
if e.code >= 500:
logger.warning("Problem occured serverside. Waiting %i seconds",
bad_sync_timeout)
sleep(bad_sync_timeout)
bad_sync_timeout = min(bad_sync_timeout * 2,
self.bad_sync_timeout_limit)
_bad_sync_timeout = min(_bad_sync_timeout * 2,
self.bad_sync_timeout_limit)
elif exception_handler is not None:
exception_handler(e)
else:
Expand Down

0 comments on commit 06ba97e

Please sign in to comment.