Skip to content

fix(p2p): update peer address on reconnect and optimize reacher#5348

Merged
gacevicljubisa merged 6 commits intomasterfrom
fix/reacher-improvements
Feb 13, 2026
Merged

fix(p2p): update peer address on reconnect and optimize reacher#5348
gacevicljubisa merged 6 commits intomasterfrom
fix/reacher-improvements

Conversation

@gacevicljubisa
Copy link
Member

@gacevicljubisa gacevicljubisa commented Feb 5, 2026

Checklist

  • I have read the coding guide.
  • My change requires a documentation update, and I have done it.
  • I have added tests to cover my changes.
  • I have filled out the description and linked the related issues.

Description

This PR improves the reacher component in two ways:

1. Optimize peer scheduling with a min-heap

Replace the map[string]*peer with a min-heap ordered by retryAfter time,
backed by an index map for O(1) lookups. The previous implementation iterated
over all peers on every scheduling cycle to find the one with the earliest
retry time. The heap reduces this to O(1) peek / O(log n) updates.

2. Update peer underlay address on reconnect

When a peer reconnects (same overlay, different underlay), the reacher now
updates the stored multiaddr and resets the retry timer to trigger an
immediate re-ping. Previously, duplicate Connected calls for the same
overlay were silently ignored, causing the reacher to keep pinging the
stale address. This is particularly important for peers that support
multiple underlays or change IP addresses between connections.

After the immediate re-ping, the next scheduled ping follows the normal
RetryAfterDuration cadence.

Open API Spec Version Changes (if applicable)

Motivation and Context (Optional)

Related Issue (Optional)

Screenshots (if appropriate):

@gacevicljubisa gacevicljubisa marked this pull request as ready for review February 9, 2026 12:56
}

// peerHeap is a min-heap of peers ordered by retryAfter time.
type peerHeap []*peer
Copy link
Contributor

@martinconic martinconic Feb 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move the heap into a separate file

defer r.wg.Done()

c := make(chan *peer)
c := make(chan peer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and also bellow, why not using a pointer to a peer anymore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By sending a value copy through the channel, each ping goroutine gets its own independent snapshot of the peer data, which is safe to read concurrently

@gacevicljubisa gacevicljubisa merged commit ffb7104 into master Feb 13, 2026
23 of 24 checks passed
@gacevicljubisa gacevicljubisa deleted the fix/reacher-improvements branch February 13, 2026 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants