Skip to content
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

feat: add an AddedAt timestamp #84

Merged
merged 1 commit into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ type PeerInfo struct {
// successful query response from the peer.
LastSuccessfulOutboundQueryAt time.Time

// AddedAt is the time this peer was added to the routing table.
AddedAt time.Time

// Id of the peer in the DHT XOR keyspace
dhtId ID
}
Expand Down
31 changes: 24 additions & 7 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,11 @@ func (rt *RoutingTable) TryAddPeer(p peer.ID, queryPeer bool) (bool, error) {
func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
bucketID := rt.bucketIdForPeer(p)
bucket := rt.buckets[bucketID]

now := time.Now()
var lastUsefulAt time.Time
if queryPeer {
lastUsefulAt = time.Now()
lastUsefulAt = now
}

// peer already exists in the Routing Table.
Expand All @@ -159,8 +161,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {

// We have enough space in the bucket (whether spawned or grouped).
if bucket.len() < rt.bucketsize {
bucket.pushFront(&PeerInfo{Id: p, LastUsefulAt: lastUsefulAt, LastSuccessfulOutboundQueryAt: time.Now(),
dhtId: ConvertPeerID(p)})
bucket.pushFront(&PeerInfo{
Id: p,
LastUsefulAt: lastUsefulAt,
LastSuccessfulOutboundQueryAt: now,
AddedAt: now,
dhtId: ConvertPeerID(p),
})
rt.PeerAdded(p)
return true, nil
}
Expand All @@ -174,8 +181,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {

// push the peer only if the bucket isn't overflowing after slitting
if bucket.len() < rt.bucketsize {
bucket.pushFront(&PeerInfo{Id: p, LastUsefulAt: lastUsefulAt, LastSuccessfulOutboundQueryAt: time.Now(),
dhtId: ConvertPeerID(p)})
bucket.pushFront(&PeerInfo{
Id: p,
LastUsefulAt: lastUsefulAt,
LastSuccessfulOutboundQueryAt: now,
AddedAt: now,
dhtId: ConvertPeerID(p),
})
rt.PeerAdded(p)
return true, nil
}
Expand All @@ -190,8 +202,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
if time.Since(minLast.LastUsefulAt) > rt.usefulnessGracePeriod {
// let's evict it and add the new peer
if bucket.remove(minLast.Id) {
bucket.pushFront(&PeerInfo{Id: p, LastUsefulAt: lastUsefulAt, LastSuccessfulOutboundQueryAt: time.Now(),
dhtId: ConvertPeerID(p)})
bucket.pushFront(&PeerInfo{
Id: p,
LastUsefulAt: lastUsefulAt,
LastSuccessfulOutboundQueryAt: now,
AddedAt: now,
dhtId: ConvertPeerID(p),
})
rt.PeerAdded(p)
return true, nil
}
Expand Down
8 changes: 7 additions & 1 deletion table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@ func TestBucket(t *testing.T) {
peers := make([]peer.ID, 100)
for i := 0; i < 100; i++ {
peers[i] = test.RandPeerIDFatal(t)
b.pushFront(&PeerInfo{peers[i], testTime1, testTime2, ConvertPeerID(peers[i])})
b.pushFront(&PeerInfo{
Id: peers[i],
LastUsefulAt: testTime1,
LastSuccessfulOutboundQueryAt: testTime2,
AddedAt: testTime1,
dhtId: ConvertPeerID(peers[i]),
})
}

local := test.RandPeerIDFatal(t)
Expand Down