Skip to content

Implement automatic reconnection for WS #1198

@danielballan

Description

@danielballan

If a websocket connection is severed due to network issues for a server restart, a Subscription should automatically reconnect, starting from the most recently processed sequence.

That is, this loop should keep track of the last sequence it has successfully received. On disconnect, it should resume from there using self._connect(start=1 + sequence). Since this can be done seamlessly, it is probably not necessary to log a warning for the user.

def _receive(self) -> None:
"Blocking loop that receives and processes updates"
while not self._close_event.is_set():
try:
data_bytes = self._websocket.recv(timeout=RECEIVE_TIMEOUT)
except (TimeoutError, anyio.EndOfStream):
continue
except websockets.exceptions.ConnectionClosedOK:
self._close_event.set()
return
data = msgpack.unpackb(data_bytes)
for ref in self._callbacks:
callback = ref()
if callback is not None:
callback(self, data)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions