chore: persist active-first peers with debounced writer #129
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.
Title
Persist active-first peers to
last_peers.jsonwith debounced writer; removelastKnownPeersSummary
This PR reworks peer persistence to make
last_peers.jsonalways reflect the most useful peers for fast bootstrap and robust recovery.It removes the legacy
lastKnownPeerslogic and replaces it with a unified, debounced persistence mechanism.Key Changes
Active-first persistence
last_peers.jsonnow lists:Peers()(currently connected peers).seenPeers), acting as fallbacks.Entries are deduplicated and capped (e.g., at 200) to prevent file bloat.
Debounced runtime writes
seenPeersand schedule a debounced write, minimizing disk churn.Graceful shutdown handling
Introduced an
isShuttingDownflag:Close()performs a final synchronous persist.Code cleanup
lastKnownPeers(fields, helpers, locks, and references).wsl,nlreturn,gofmt).Issue Addressed
Previously, last_peers.json could be overwritten with stale data during shutdown, leading to:
Slow reconnections.
Dialing of dead or outdated peers.
This update ensures last_peers.json always reflects a current, prioritized, and bounded set of peers, improving reconnection reliability and startup speed.
Impact
Behavior
Compatibility
[]stringof multiaddrs).Performance
Testing & Validation
Updated unit tests to validate internal peer tracking (
numPeers()instead ofPeers()where relevant).Manual validation steps:
Start a small cluster, connect/disconnect peers.
Confirm
last_peers.jsonordering: active peers first, inactive peers next.Graceful shutdown logs:
and shows active-first ordering.
Restart nodes with only
last_peers.json: observe dialing prioritizes live peers.