-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NPeersForCpl and collapse empty buckets #77
Changes from 6 commits
cc56205
c52371e
40bab45
9ee07ae
ee0822c
f3c8e92
2e988e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,26 @@ func (rt *RoutingTable) Close() error { | |
return nil | ||
} | ||
|
||
// NPeersForCPL returns the number of peers we have for a given Cpl | ||
func (rt *RoutingTable) NPeersForCpl(cpl uint) int { | ||
aschmahmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rt.tabLock.RLock() | ||
defer rt.tabLock.RUnlock() | ||
|
||
// it's in the last bucket | ||
if int(cpl) >= len(rt.buckets)-1 { | ||
count := 0 | ||
b := rt.buckets[len(rt.buckets)-1] | ||
for _, p := range b.peers() { | ||
if CommonPrefixLen(rt.local, p.dhtId) == int(cpl) { | ||
count++ | ||
} | ||
} | ||
return count | ||
} else { | ||
return rt.buckets[cpl].len() | ||
} | ||
} | ||
|
||
// TryAddPeer tries to add a peer to the Routing table. If the peer ALREADY exists in the Routing Table, this call is a no-op. | ||
// If the peer is a queryPeer i.e. we queried it or it queried us, we set the LastSuccessfulOutboundQuery to the current time. | ||
// If the peer is just a peer that we connect to/it connected to us without any DHT query, we consider it as having | ||
|
@@ -117,6 +137,11 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) { | |
|
||
// peer already exists in the Routing Table. | ||
if peer := bucket.getPeer(p); peer != nil { | ||
// if we're querying the peer first time after adding it, let's give it a | ||
// usefulness bump. This will ONLY happen once. | ||
if peer.LastUsefulAt.IsZero() && queryPeer { | ||
peer.LastUsefulAt = lastUsefulAt | ||
} | ||
Comment on lines
+146
to
+150
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this fix related to the rest of the PR, or just a "while I'm already here" kind of thing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hey, This change is not related to the rest of the PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem, while you're here and changing this do you mind add more information to the |
||
return false, nil | ||
} | ||
|
||
|
@@ -228,9 +253,25 @@ func (rt *RoutingTable) removePeer(p peer.ID) { | |
bucketID := rt.bucketIdForPeer(p) | ||
bucket := rt.buckets[bucketID] | ||
if bucket.remove(p) { | ||
for { | ||
lastBucketIndex := len(rt.buckets) - 1 | ||
|
||
// remove the last bucket if it's empty and it isn't the only bucket we have | ||
if len(rt.buckets) > 1 && rt.buckets[lastBucketIndex].len() == 0 { | ||
rt.buckets[lastBucketIndex] = nil | ||
rt.buckets = rt.buckets[:lastBucketIndex] | ||
} else if len(rt.buckets) >= 2 && rt.buckets[lastBucketIndex-1].len() == 0 { | ||
// if the second last bucket just became empty, remove and replace it with the last bucket. | ||
rt.buckets[lastBucketIndex-1] = rt.buckets[lastBucketIndex] | ||
rt.buckets[lastBucketIndex] = nil | ||
rt.buckets = rt.buckets[:lastBucketIndex] | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
// peer removed callback | ||
rt.PeerRemoved(p) | ||
return | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you get away without this function, and just call
len(GetPeersForCpl)
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have removed
GetPeersForCpl
as we don't need it anymore.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've removed
GetPeersForCpl
as we don't need it anymore.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this is a tiny bit more efficient then just
len(GetPeersForCpl)
and we can always addGetPeersForCpl
back in if we need it. Your call 😄