fix(kad-dht): prefer connected peers to reduce unnecessary dials#3418
Open
paschal533 wants to merge 4 commits intolibp2p:mainfrom
Open
fix(kad-dht): prefer connected peers to reduce unnecessary dials#3418paschal533 wants to merge 4 commits intolibp2p:mainfrom
paschal533 wants to merge 4 commits intolibp2p:mainfrom
Conversation
faa87be to
395c833
Compare
Sort CIDs by their Kademlia key before queueing reprovides so that XOR-adjacent CIDs are processed consecutively. Nearby CIDs in the keyspace share the same K closest peers, so connections opened for one CID are likely to be reused for the next, reducing the number of new dials per reprovide run (Amino DHT SweepingProvider optimisation).
395c833 to
968206e
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Reduces unnecessary new dials in the Amino DHT by preferring already-connected peers at three levels:
Tier 1 Query manager: connected-peers-first path distribution (
query/manager.ts)Before distributing the K closest routing-table peers into D disjoint query paths, sort them so already-connected peers come first. With a round-robin bucket assignment, each path gets a connected peer as its starting point, reducing the cold-dial penalty at the beginning of every DHT query.
Tier 2 Content routing: connected-peers-first ADD_PROVIDER fan-out (
content-routing/index.ts)After collecting the K closest
FINAL_PEERevents duringprovide(), sort them by connection status before enqueuing the parallel ADD_PROVIDER sends. The first ALPHA queue slots go to already-connected peers, so provider records reach the network with fewer new dials.Tier 3 Reprovider: Kademlia key order sweep (
reprovider.ts)Port go-libp2p's SweepingProvider optimisation.
processRecordsnow collects all CIDs that need reproviding, computes their Kademlia keys, sorts them lexicographically, and queues reprovides in that order. XOR-adjacent CIDs share the same K closest peers, so connections opened for one CID are reused for the next, reducing the total number of new dials across a reprovide run.Test plan
query.spec.tsverifies each disjoint path gets at least one connected starting peer when connected peers are availablecontent-routing.spec.tsverifiesconnectionManager.getConnectionsis called duringprovide()for the connected-first sortreprovider.spec.tsinserts 5 CIDs in reverse Kademlia key order, then verifiescontentRouting.provideis called in ascending Kademlia key order