-
Notifications
You must be signed in to change notification settings - Fork 1
Add PING/PONG keep-alive functionality #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Warning Rate limit exceeded@lalinsky has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 16 minutes and 52 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. 📒 Files selected for processing (1)
WalkthroughAdds timer-driven keep-alive PING/PONG to Connection: configurable ping interval and max outstanding pings, atomic outstanding-pings counter, StaleConnection error when limit exceeded, handshake now emits CONNECT+PING, and PONG resets the outstanding-pings counter. (≤50 words) Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Assessment against linked issues
Possibly related PRs
✨ Finishing Touches🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (3)
src/connection.zig (3)
185-187: Clarify/adjust max_pings_out=0 semantics.As written, a value of 0 will trigger StaleConnection on the very first keep-alive PING. If the intent is “unlimited” (common pattern), gate the stale check only when max_pings_out > 0 and document it.
Would you like 0 to mean “unlimited outstanding keep-alive PINGs”? If yes, the diff in my checkAndSendPing comment below implements that.
230-233: Fields look right; reset the timer on (re)connect to avoid immediate PING.Without a reset, a long-lived Connection instance can fire a keep-alive PING immediately during handshake. Suggest lapping the timer after establishing a socket.
Apply after the existing ping/pong counter reset:
@@ - // Reset ping/pong counters for fresh connection - self.outgoing_pings = 0; - self.incoming_pongs = 0; + // Reset ping/pong counters for fresh connection + self.outgoing_pings = 0; + self.incoming_pongs = 0; + // Reset keep-alive timer so we don't fire immediately on a fresh connection + _ = self.ping_timer.lap();
1302-1304: Lap the timer on any PONG to avoid back-to-back PINGs after long stalls.Prevents sending another keep-alive immediately after we just heard from the server.
- // Reset keep-alive ping counter - ANY PONG proves connection is alive - self.pings_out.store(0, .monotonic); + // Reset keep-alive ping counter - ANY PONG proves connection is alive + self.pings_out.store(0, .monotonic); + _ = self.ping_timer.lap(); // restart the keep-alive interval
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/connection.zig(7 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.zig
📄 CodeRabbit inference engine (CLAUDE.md)
Format code using
zig fmtbefore commit
Files:
src/connection.zig
🔇 Additional comments (2)
src/connection.zig (2)
116-117: Adding StaleConnection to ConnectionError — good call.Fits the reconnection flow and keeps the failure mode explicit.
268-269: Timer init: fine.Starting the timer here is OK given the lap-on-(re)connect suggestion above.
225d0fc to
0764f6d
Compare
- Add ping_interval_ms (default 2min) and max_pings_out (default 2) config options - Add ping_timer and atomic pings_out counter to Connection struct - Implement checkAndSendPing() helper that sends PINGs at intervals - Update readerIteration() to check for needed PINGs after each read - Reset pings_out counter to 0 on any PONG (proves connection alive) - Add StaleConnection error for when too many PINGs go unanswered - Follows C client pattern with separate counters for keep-alive vs flush PINGs - Lock-free implementation using atomic operations for performance
0764f6d to
c697a02
Compare
Summary
This PR implements PING/PONG keep-alive functionality for the NATS Zig client, following the same pattern as the C client.
Fixes #88
Key Features
ping_interval_ms(default: 2 minutes, 0 = disabled)max_pings_out(default: 2 unanswered PINGs)Implementation Details
ping_timerand atomicpings_outcounter to Connection structcheckAndSendPing()helper that sends PINGs at configured intervalsreaderIteration()to check for needed PINGs after each readpings_outcounter on any PONG (flush or keep-alive proves connection alive)StaleConnectionerror for when too many PINGs go unansweredDesign Decisions
pings_out) vs flush PINGs (outgoing_pings/incoming_pongs)std.time.Timer.lap()for efficient interval tracking