Unverified Commit b37bee77 authored by Steven Allen's avatar Steven Allen Committed by GitHub

feat: add an AddedAt timestamp (#84)

`AddedAt` field for a peer in the Routing Table.
parent 260be2a2
......@@ -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
}
......
......@@ -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.
......@@ -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
}
......@@ -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
}
......@@ -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
}
......
......@@ -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)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment