[management] fix set disconnected status for connected peer#5247
Conversation
📝 WalkthroughWalkthroughThis pull request threads a new Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant GRPC_Server as gRPC Server
participant SyncHandler as Sync/handleUpdates
participant AccountMgr as AccountManager
participant Store as Peer Store
Client->>GRPC_Server: open stream (records streamStartTime)
GRPC_Server->>SyncHandler: start sync (streamStartTime)
SyncHandler->>SyncHandler: process updates
alt initial-sync error or disconnect condition
SyncHandler->>GRPC_Server: trigger cleanup (streamStartTime)
GRPC_Server->>AccountMgr: OnPeerDisconnected(ctx, accountID, peerPubKey, streamStartTime)
AccountMgr->>Store: GetPeerByPeerPubKey(peerPubKey)
Store-->>AccountMgr: peer (with LastSeen)
alt peer.Status.LastSeen <= streamStartTime
AccountMgr->>Store: mark peer disconnected
else peer.Status.LastSeen > streamStartTime
AccountMgr-->>GRPC_Server: skip disconnect (recent activity)
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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.
Pull request overview
This PR updates the peer disconnection handling so that peers are not incorrectly marked as disconnected when a newer connection/session is already active.
Changes:
- Extends the
Manager.OnPeerDisconnectedinterface and its mock/implementation to accept astreamStartTimeparameter. - In the gRPC
Syncflow, tracks a per-stream timestamp and threads it through the update/send/cancellation pipeline. - In
DefaultAccountManager.OnPeerDisconnected, adds a guard that checks the peer’sLastSeenagainst the stream’s start time before marking the peer as disconnected.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
management/server/mock_server/account_mock.go |
Updates the mock account manager to implement the new OnPeerDisconnected signature with a no-op body so tests can compile against the new interface. |
management/server/account/manager.go |
Updates the Manager interface’s OnPeerDisconnected signature to include the streamStartTime parameter used for race-avoidance logic. |
management/server/account.go |
Implements time-based guarding in DefaultAccountManager.OnPeerDisconnected to skip disconnect when a peer has newer activity than the current stream, and delegates disconnection to MarkPeerConnected(..., false, ...). |
management/internals/shared/grpc/server.go |
Captures a per-stream timestamp and passes it to handleUpdates, sendUpdate, and the peer cancellation helpers, integrating the account manager’s new disconnection semantics into the gRPC sync pipeline. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|



Describe your changes
Issue ticket number and link
Stack
Checklist
Documentation
Select exactly one:
bug fix
Docs PR URL (required if "docs added" is checked)
Paste the PR link from https://github.com/netbirdio/docs here:
https://github.com/netbirdio/docs/pull/__
Summary by CodeRabbit