Resilient connections: auto-reconnect and heartbeat#80
Merged
Conversation
Add a :ping handler that replies with :pong, so the client can use an application-level heartbeat to detect a silently dead connection, plus a :default no-op so an unknown op never blows up the server's message loop.
The client now reconnects on its own after an unexpected drop (page reload, server restart, flaky link) using an exponential backoff that only resets once a socket proves stable, and exposes a new disconnect fn to stop for good. An optional, opt-in heartbeat detects a silently dead link and forces a reconnect; it tolerates a few missed pongs before giving up and never disturbs a server that doesn't answer pings. Connection state lives in a single generation-guarded atom so superseded sockets can't corrupt a fresh session, timers never leak across connects, and alive? stays honest. The Node integration test bounces the server mid-session and asserts the client reconnects and round-trips again, and checks the heartbeat keeps pinging without tearing down a healthy socket.
a438c56 to
b047efa
Compare
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.
Second functionality improvement, built on the native-WebSocket client. The REPL connection now heals itself.
weasel.repl/disconnectcloses for good and stops retrying.:heartbeat-interval): an app-level:ping/:pongdetects a silently dead link and forces a reconnect. It negotiates - a server that never pongs is never disturbed - and the server gained a:pinghandler.Connection state lives in one generation-guarded atom, so superseded sockets can't corrupt a fresh session, timers don't leak across
connectcalls, andalive?stays honest through failed connects anddisconnect.Worth a careful look: I ran an automated review on the first cut and it found a pile of lifecycle bugs (heartbeat killing healthy sockets, an
:occupiedreconnect loop, leaked timers,alive?lying). Those are all fixed here, but the reconnect/teardown logic is the part to scrutinize. The Node integration test bounces the server mid-session and asserts eval, print, heartbeat and reconnect all work end to end; a unit test covers the server's pong.