Repository navigation
Conversation
The client dropped a connection with "no ping" once ping interval + max_server_ping_delay passed without a ping, whatever else arrived meanwhile. A busy server may skip pings while it writes other messages, so a connection that kept delivering publications was dropped every ping interval + delay. centrifuge-js restarts its server ping wait on every batch of replies it receives; this does the same. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Hi @damirych I'd prefer not to merge this right away. In Centrifuge, server pings go through the same per-connection write queue as publications, so the server never skips a ping because it's busy. A ping arrives late only when it's queued behind other messages, which means the connection or the client isn't keeping up. Then publications are delayed by the same amount. Reconnecting on "no ping" in that case is generally intended: it's a form of backpressure, and it usually points to an application that can't process the stream fast enough (e.g. slow on_publication handlers). You're right that centrifuge-js restarts the wait on any data. That's an inconsistency between our SDKs (centrifuge-go waits for pings only). Probably it was a mistake to introduce this in centrifuge-js, and better to align with the rest of SDKs removing the restart instead of adding it in other SDKs 🤔 Sth to think about. Getting no pings for ~30 seconds while publications keep arriving is unusual, though. If this happens without slow app, then maybe it's a signal of wrong server behavior. Would be nice to see a reproducer. |
|
Thanks for the explanation. Measurements confirm it, so this PR is closed. A probe ran for 24 hours on the same machine as the SDK client: two raw JSON connections to the same Centrifugo 5.4.9 server (behind Cloudflare). It recorded every server ping, the round-trip time (RTT) of a WebSocket ping sent every 10 seconds, and the delay of each publication against a timestamp in its payload.
Restarting the ping wait on data would only keep a connection that is tens of seconds behind for longer. The earlier observation of no pings while publications kept arriving was the same backlog seen from the client side. 🤖 Generated with Claude Code |
What changed
Client._process_incoming_datanow restarts the server ping wait (_restart_ping_wait()) for every frame that carries at least one reply, as long as the wait is armed. Before, only a ping (_handle_ping) and a successful connect restarted it.Why
The client drops a connection with "no ping" once
ping interval + max_server_ping_delaypasses without a ping, whatever else arrived meanwhile. A busy server may not ping while it is writing other messages, and then a connection that keeps delivering publications is dropped every 35 seconds (withping: 25and the default delay of 10).I ran into this against a production Centrifugo 5.4.9 (connect reply
ping: 25, pong: true) on a busy public channel:DisconnectedContext(code=2, reason='no ping').I have not checked the server code to see why pings stop under load. The client side does not depend on it: centrifuge-js already restarts its server ping wait on any replies, in
_dataReceived():This may be what #33 ran into as well (a high-traffic client, with "no ping" among the disconnect reasons).
Only an armed wait is restarted.
_do_disconnectcancels the timer and sets_ping_timer = None, then awaits the cancelled listen task. The message-processing task can still handle a queued frame in that window, and restarting the wait there would arm a new timer after the disconnect has cleared it, which could then fire as a second "no ping" during a slow reconnect._handle_pinghas the same window for pings. I left it as it is to keep this change to one thing.Verification
Added
tests/test_server_ping.py, which needs no Docker (it uses the in-process fake server, which never sends pings). The connect reply setsping: 1and the clientmax_server_ping_delay=0.5, so the client waits 1.5 seconds for a ping:test_data_without_pings_keeps_the_connectionpublishes every 0.2 seconds for 3 seconds and asserts that the client never disconnected and is stillCONNECTED. Onmasterit fails with[(2, 'no ping')] != [].test_silence_still_ends_the_connectionsends nothing for 2.5 seconds and asserts that the client disconnected with(2, "no ping"). It passes on both, and guards the watchdog itself against this change.Also ran:
tests/test_client.pyagainst this repo's docker-compose Centrifugo: 62 tests OK.test_filter,test_fossil,test_proxy,test_ssl,test_state_invalidation,test_sub_refresh,test_background_tasks,test_ready,test_refresh,test_unsubscribe,test_utils) together with the new one: 63 tests OK.ruff check .andruff format --check: clean.The same change, as a
Clientsubclass override in an application, saw no "no ping" drops against that server in a 150-second busy run, in which the server sent a single ping.🤖 Generated with Claude Code