Skip to content

Commit 6915276

Browse files
committed
address cr feedback
And tag useful peers in higher buckets
1 parent 371d4dc commit 6915276

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

dht.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,23 @@ var (
4242
baseLogger = logger.Desugar()
4343
)
4444

45-
// BaseConnMgrScore is the base of the score set on the connection manager "kbucket" tag.
46-
// It is added with the common prefix length between two peer IDs.
47-
const BaseConnMgrScore = 5
45+
const (
46+
// BaseConnMgrScore is the base of the score set on the connection
47+
// manager "kbucket" tag. It is added with the common prefix length
48+
// between two peer IDs.
49+
baseConnMgrScore = 5
50+
51+
// UsefulConnMgrScore is given to peers that are among the first peers
52+
// to respond to a query.
53+
//
54+
// This score is given to peers the first time they're useful and lasts
55+
// until we disconnect from the peer.
56+
usefulConnMgrScore = 20
57+
58+
// UsefulConnMgrProtectedBuckets is the number of buckets where useful
59+
// peers are _protected_, instead of just given the useful score.
60+
usefulConnMgrProtectedBuckets = 2
61+
)
4862

4963
type mode int
5064

@@ -347,8 +361,14 @@ func makeRoutingTable(dht *IpfsDHT, cfg config, maxLastSuccessfulOutboundThresho
347361
cmgr := dht.host.ConnManager()
348362

349363
rt.PeerAdded = func(p peer.ID) {
364+
// We tag our closest peers with higher and higher scores so we
365+
// stay connected to our nearest neighbors.
366+
//
367+
// We _also_ (elsewhere) protect useful peers in the furthest
368+
// buckets (our "core" routing nodes) and give high scores to
369+
// all other useful peers.
350370
commonPrefixLen := kb.CommonPrefixLen(dht.selfKey, kb.ConvertPeerID(p))
351-
cmgr.TagPeer(p, kbucketTag, BaseConnMgrScore+commonPrefixLen)
371+
cmgr.TagPeer(p, kbucketTag, baseConnMgrScore+commonPrefixLen)
352372
}
353373
rt.PeerRemoved = func(p peer.ID) {
354374
cmgr.Unprotect(p, dhtUsefulTag)

query.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,11 @@ func (q *query) recordPeerIsValuable(p peer.ID) {
186186
// Restrict to buckets 0, 1 (75% of requests, max 40 peers), so we don't
187187
// protect _too_ many peers.
188188
commonPrefixLen := kb.CommonPrefixLen(q.dht.selfKey, kb.ConvertPeerID(p))
189+
cmgr := q.dht.host.ConnManager()
189190
if commonPrefixLen < 2 {
190-
q.dht.host.ConnManager().Protect(p, dhtUsefulTag)
191+
cmgr.Protect(p, dhtUsefulTag)
192+
} else {
193+
cmgr.TagPeer(p, dhtUsefulTag, usefulConnMgrScore)
191194
}
192195
}
193196

0 commit comments

Comments
 (0)