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 { ...@@ -21,6 +21,9 @@ type PeerInfo struct {
// successful query response from the peer. // successful query response from the peer.
LastSuccessfulOutboundQueryAt time.Time 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 // Id of the peer in the DHT XOR keyspace
dhtId ID dhtId ID
} }
......
...@@ -136,9 +136,11 @@ func (rt *RoutingTable) TryAddPeer(p peer.ID, queryPeer bool) (bool, error) { ...@@ -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) { func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
bucketID := rt.bucketIdForPeer(p) bucketID := rt.bucketIdForPeer(p)
bucket := rt.buckets[bucketID] bucket := rt.buckets[bucketID]
now := time.Now()
var lastUsefulAt time.Time var lastUsefulAt time.Time
if queryPeer { if queryPeer {
lastUsefulAt = time.Now() lastUsefulAt = now
} }
// peer already exists in the Routing Table. // peer already exists in the Routing Table.
...@@ -159,8 +161,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) { ...@@ -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). // We have enough space in the bucket (whether spawned or grouped).
if bucket.len() < rt.bucketsize { if bucket.len() < rt.bucketsize {
bucket.pushFront(&PeerInfo{Id: p, LastUsefulAt: lastUsefulAt, LastSuccessfulOutboundQueryAt: time.Now(), bucket.pushFront(&PeerInfo{
dhtId: ConvertPeerID(p)}) Id: p,
LastUsefulAt: lastUsefulAt,
LastSuccessfulOutboundQueryAt: now,
AddedAt: now,
dhtId: ConvertPeerID(p),
})
rt.PeerAdded(p) rt.PeerAdded(p)
return true, nil return true, nil
} }
...@@ -174,8 +181,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) { ...@@ -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 // push the peer only if the bucket isn't overflowing after slitting
if bucket.len() < rt.bucketsize { if bucket.len() < rt.bucketsize {
bucket.pushFront(&PeerInfo{Id: p, LastUsefulAt: lastUsefulAt, LastSuccessfulOutboundQueryAt: time.Now(), bucket.pushFront(&PeerInfo{
dhtId: ConvertPeerID(p)}) Id: p,
LastUsefulAt: lastUsefulAt,
LastSuccessfulOutboundQueryAt: now,
AddedAt: now,
dhtId: ConvertPeerID(p),
})
rt.PeerAdded(p) rt.PeerAdded(p)
return true, nil return true, nil
} }
...@@ -190,8 +202,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) { ...@@ -190,8 +202,13 @@ func (rt *RoutingTable) addPeer(p peer.ID, queryPeer bool) (bool, error) {
if time.Since(minLast.LastUsefulAt) > rt.usefulnessGracePeriod { if time.Since(minLast.LastUsefulAt) > rt.usefulnessGracePeriod {
// let's evict it and add the new peer // let's evict it and add the new peer
if bucket.remove(minLast.Id) { if bucket.remove(minLast.Id) {
bucket.pushFront(&PeerInfo{Id: p, LastUsefulAt: lastUsefulAt, LastSuccessfulOutboundQueryAt: time.Now(), bucket.pushFront(&PeerInfo{
dhtId: ConvertPeerID(p)}) Id: p,
LastUsefulAt: lastUsefulAt,
LastSuccessfulOutboundQueryAt: now,
AddedAt: now,
dhtId: ConvertPeerID(p),
})
rt.PeerAdded(p) rt.PeerAdded(p)
return true, nil return true, nil
} }
......
...@@ -34,7 +34,13 @@ func TestBucket(t *testing.T) { ...@@ -34,7 +34,13 @@ func TestBucket(t *testing.T) {
peers := make([]peer.ID, 100) peers := make([]peer.ID, 100)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
peers[i] = test.RandPeerIDFatal(t) 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) 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