Skip to content

Commit

Permalink
Optional connection retries (Fixes #1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Feb 5, 2024
1 parent 5f00188 commit b4f3614
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/socketio/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 69,7 @@ def is_asyncio_based(self):

async def connect(self, url, headers={}, auth=None, transports=None,
namespaces=None, socketio_path='socket.io', wait=True,
wait_timeout=1):
wait_timeout=1, retry=False):
"""Connect to a Socket.IO server.
:param url: The URL of the Socket.IO server. It can include custom
Expand Down Expand Up @@ -105,6 105,8 @@ async def connect(self, url, headers={}, auth=None, transports=None,
connection. The default is 1 second. This
argument is only considered when ``wait`` is set
to ``True``.
:param retry: Apply the reconnection logic if the initial connection
attempt fails. The default is ``False``.
Note: this method is a coroutine.
Expand Down Expand Up @@ -147,6 149,10 @@ async def connect(self, url, headers={}, auth=None, transports=None,
await self._trigger_event(
'connect_error', n,
exc.args[1] if len(exc.args) > 1 else exc.args[0])
if retry: # pragma: no cover
await self._handle_reconnect()
if self.eio.state == 'connected':
return
raise exceptions.ConnectionError(exc.args[0]) from None

if wait:
Expand Down Expand Up @@ -477,7 483,8 @@ async def _handle_reconnect(self):
auth=self.connection_auth,
transports=self.connection_transports,
namespaces=self.connection_namespaces,
socketio_path=self.socketio_path)
socketio_path=self.socketio_path,
retry=False)
except (exceptions.ConnectionError, ValueError):
pass
else:
Expand Down
11 changes: 9 additions & 2 deletions src/socketio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 69,7 @@ class Client(base_client.BaseClient):
"""
def connect(self, url, headers={}, auth=None, transports=None,
namespaces=None, socketio_path='socket.io', wait=True,
wait_timeout=1):
wait_timeout=1, retry=False):
"""Connect to a Socket.IO server.
:param url: The URL of the Socket.IO server. It can include custom
Expand Down Expand Up @@ -105,6 105,8 @@ def connect(self, url, headers={}, auth=None, transports=None,
connection. The default is 1 second. This
argument is only considered when ``wait`` is set
to ``True``.
:param retry: Apply the reconnection logic if the initial connection
attempt fails. The default is ``False``.
Example usage::
Expand Down Expand Up @@ -145,6 147,10 @@ def connect(self, url, headers={}, auth=None, transports=None,
self._trigger_event(
'connect_error', n,
exc.args[1] if len(exc.args) > 1 else exc.args[0])
if retry: # pragma: no cover
self._handle_reconnect()
if self.eio.state == 'connected':
return
raise exceptions.ConnectionError(exc.args[0]) from None

if wait:
Expand Down Expand Up @@ -441,7 447,8 @@ def _handle_reconnect(self):
auth=self.connection_auth,
transports=self.connection_transports,
namespaces=self.connection_namespaces,
socketio_path=self.socketio_path)
socketio_path=self.socketio_path,
retry=False)
except (exceptions.ConnectionError, ValueError):
pass
else:
Expand Down

0 comments on commit b4f3614

Please sign in to comment.