Implement auto-reconnection for SUB sockets#230
Merged
Conversation
This commit adds automatic reconnection support to zmq.rs SUB sockets when connected peers disconnect and reconnect. When a connection is lost, a background task attempts to reconnect with exponential backoff (100ms to 30s), and on successful reconnection, subscriptions are automatically re-sent to restore the subscription state. The implementation includes: - New src/reconnect.rs module with ReconnectConfig and spawn_reconnect_task - FairQueue enhancements: on_disconnect callback to detect peer disconnections - Backend shutdown fix: clear fair_queue streams to ensure TCP connections close - SubSocketBackend with disconnect notifier support for reconnection tasks - SubSocket override of connect() to spawn background reconnection monitors - Updated reconnection_compliant test that now passes (previously ignored) All CI checks pass: formatting, clippy (--deny warnings), and tests across all three runtime backends (tokio, async-std, async-dispatcher).
Address three issues identified in code review: 1. Arc cycle fix: Use Arc::downgrade() in fair_queue on_disconnect callback to capture a Weak reference instead of a strong Arc. This prevents the cycle: backend -> fair_queue_inner -> callback -> backend. 2. Shutdown check in retry loop: Add shutdown signal check during backoff sleep using futures::select!. The shutdown_rx is now fused so it can be polled multiple times. This ensures ReconnectHandle::shutdown() works even when the endpoint is down and retries are in progress. 3. Emit Connected event on reconnect: try_reconnect now returns the resolved endpoint, and the reconnect task emits SocketEvent::Connected after successful reconnection. This ensures monitor consumers see matching Connected/Disconnected events.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements automatic reconnection support for zmq.rs SUB sockets. When a connected peer disconnects, a background task automatically reconnects with exponential backoff, and subscriptions are automatically re-sent on reconnection. This restores standard ZeroMQ behavior where SUB sockets maintain connectivity.
Implementation Details
src/reconnect.rsmodule providingReconnectConfigandspawn_reconnect_task