Skip to content

Commit

Permalink
fix: Reset the client's state, before scheduling the reconnection, ra…
Browse files Browse the repository at this point in the history
…ther than immediately before reconnecting

This allows clients to e.g. track when the client is ready to publish messages.
  • Loading branch information
jpmckinney committed Jan 24, 2024
1 parent 63db184 commit 7e0c564
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Fixed
~~~~~

- If the client attempts to close the connection while it is opening, do not retry opening the connection.
- Reset the client's state, before scheduling the reconnection, rather than immediately before reconnecting.

0.1.3 (2023-07-03)
------------------
Expand Down
6 changes: 3 additions & 3 deletions yapw/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,6 @@ def reconnect(self) -> None:
if self.stopping:
self.connection.ioloop.stop()
else:
self.reset()
self.connect()

def reset(self) -> None:
Expand All @@ -444,17 +443,18 @@ def connection_open_error_callback(self, connection: pika.connection.Connection,
else:
logger.error("Connection failed, retrying in %ds: %r", self.RECONNECT_DELAY, error)
self.connection.ioloop.call_later(self.RECONNECT_DELAY, self.reconnect)
self.reset()

def connection_close_callback(self, connection: pika.connection.Connection, reason: Exception) -> None:
"""Reconnect, if the connection was closed unexpectedly. Otherwise, stop the IO loop."""
if self.stopping:
# A message has been logged, prior to calling interrupt().
self.connection.ioloop.stop()
else:
# For example: ConnectionClosedByBroker: (320) "CONNECTION_FORCED - broker forced connection closure with
# reason 'shutdown'"
# ConnectionClosedByBroker "CONNECTION_FORCED - broker forced connection closure with reason 'shutdown'"
logger.warning("Connection closed, reconnecting in %ds: %r", self.RECONNECT_DELAY, reason)
self.connection.ioloop.call_later(self.RECONNECT_DELAY, self.reconnect)
self.reset()

def add_signal_handler(self, signalnum: int, handler: Callable[..., object]) -> None:
"""
Expand Down

0 comments on commit 7e0c564

Please sign in to comment.