Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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