feat: simplify keepalive protocol interface#1381
Conversation
Signed-off-by: cryptodj413 <shinjirohara2@gmail.com>
📝 WalkthroughWalkthroughThe changes refactor the keep-alive protocol package's public API by replacing explicit function type fields in Config (KeepAliveFunc, KeepAliveResponseFunc, DoneFunc) with callback fields (OnKeepAliveReceived, OnKeepAliveResponseReceived). Callbacks are modified to receive ConnectionId and cookie values instead of CallbackContext. CallbackContext is expanded to include ConnectionId, Client, and Server fields. The Client and Server types gain Stop() methods, and the KeepAlive struct gains Start() and Stop() methods. Configuration builder functions are renamed accordingly, and Server.handleDone() signature changes from returning error to void. Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Pre-merge checks and finishing touches✅ Passed checks (2 passed)
✨ Finishing touches
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
protocol/keepalive/keepalive.go (1)
115-123: Consider adding aStart()method toServerfor symmetry.
Client.Start()usessync.Oncefor idempotent startup, butServerlacks a customStart()method—callingk.Server.Start()invokes the embeddedProtocol.Start()directly. IfKeepAlive.Start()is called multiple times, the client is protected but the server may not be. Consider adding aServer.Start()method with similarsync.Onceprotection for consistent behavior.#!/bin/bash # Check if Protocol.Start() has built-in idempotency protection ast-grep --pattern $'func ($_ *Protocol) Start() { $$$ }'
📜 Review details
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
protocol/keepalive/client.goprotocol/keepalive/keepalive.goprotocol/keepalive/server.go
🧰 Additional context used
🧬 Code graph analysis (3)
protocol/keepalive/server.go (3)
protocol/keepalive/keepalive.go (1)
NewConfig(139-149)protocol/keepalive/messages.go (1)
NewMsgKeepAliveResponse(78-86)protocol/handshake/server.go (1)
Server(26-30)
protocol/keepalive/keepalive.go (4)
connection.go (1)
ConnectionId(56-56)connection/id.go (1)
ConnectionId(22-25)protocol/keepalive/client.go (1)
Client(26-33)protocol/keepalive/server.go (1)
Server(24-28)
protocol/keepalive/client.go (3)
protocol/handshake/client.go (1)
Client(26-31)connection.go (1)
ConnectionId(56-56)connection/id.go (1)
ConnectionId(22-25)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
- GitHub Check: cubic · AI code reviewer
🔇 Additional comments (10)
protocol/keepalive/client.go (2)
118-127: LGTM! Clean implementation of Stop().The method properly acquires the mutex before stopping and nilling the timer, then releases before calling
Protocol.Stop(). This avoids holding the lock during potentially blocking operations.
163-167: LGTM! Callback invocation is correct.The simplified callback signature
(ConnectionId, uint16)aligns with the new interface design. The nil check onc.configis defensive sinceNewClientguarantees a non-nil config, but it's harmless.protocol/keepalive/server.go (4)
66-67: Verify that ignoring thehandleDoneresult is intentional.The previous implementation likely returned an error from
handleDone(). NowhandleDone()returns nothing and its call result is ignored while other message types still return errors viaerr. Confirm this is the intended behavior—ifhandleDonecould fail (e.g., during cleanup), any error would now be silently ignored.
88-97: Good simplification—server now always responds.The callback is now purely for notification, and the keep-alive response is always sent automatically. This is a cleaner design that removes the burden of response handling from callback implementers. Users who previously relied on
KeepAliveFuncto conditionally suppress responses will need to adapt.
32-35: LGTM! Consistent nil-config handling.Mirrors the same pattern used in
NewClient, ensuring both constructors safely default when no config is provided.
110-113: LGTM!Clean delegation to the underlying protocol. Unlike the client, the server has no timer to clean up.
protocol/keepalive/keepalive.go (4)
90-97: LGTM! Cleaner callback interface.The new signatures
func(connection.ConnectionId, uint16)are simpler and provide exactly the information callers typically need. RemovingCallbackContextfrom the callback signature reduces coupling while still providing theConnectionIdand cookie value.
125-133: LGTM! Clean shutdown logic.The nil checks prevent nil pointer dereferences.
Client.Stop()properly cleans up its timer. EnsureProtocol.Stop()(called by both) handles being invoked multiple times gracefully.
151-163: LGTM! Clear option function naming.The
WithOnKeepAliveReceivedandWithOnKeepAliveResponseReceivednames clearly indicate they set notification callbacks, aligning well with the simplified interface design.
99-104: LGTM!
CallbackContextis retained for internal use (logging, state tracking) while callbacks receive only the necessaryConnectionIdand cookie values. This is a clean separation of concerns.
ref: #108
Summary by cubic
Simplified the keepalive protocol interface by replacing message-handling callbacks with optional notification hooks and making the server auto-reply. Added Start/Stop methods for better lifecycle control and safe timer cancellation.
Refactors
New Features
Written for commit 162c37c. Summary will update on new commits.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.