@@ -109,7 +109,7 @@ type IpfsDHT struct {
109109 enableProviders , enableValues bool
110110
111111 // maxLastSuccessfulOutboundThreshold is the max threshold/upper limit on the time duration
112- // between the current time and the last time a peer was useful to us .
112+ // between the current time and the last time we successfully queried a peer .
113113 maxLastSuccessfulOutboundThreshold float64
114114
115115 fixLowPeersChan chan struct {}
@@ -448,22 +448,37 @@ func (dht *IpfsDHT) putLocal(key string, rec *recpb.Record) error {
448448
449449// peerFound signals the routingTable that we've found a peer that
450450// might support the DHT protocol.
451+ // If we have a connection a peer but no exchange of a query RPC ->
452+ // LastQueriedAt=time.Now (so we don't ping it for some time for a liveliness check)
453+ // LastUsefulAt=N/A
454+ // If we connect to a peer and exchange a query RPC ->
455+ // LastQueriedAt=time.Now (same reason as above)
456+ // LastUsefulAt=time.Now (so we give it some life in the RT without immediately evicting it)
457+ // If we query a peer we already have in our Routing Table ->
458+ // LastQueriedAt=time.Now()
459+ // LastUsefulAt remains unchanged
460+ // If we connect to a peer we already have in the RT but do not exchange a query (rare)
461+ // Do Nothing.
451462func (dht * IpfsDHT ) peerFound (ctx context.Context , p peer.ID , queryPeer bool ) {
452463 logger .Debugw ("peer found" , "peer" , p )
453464 b , err := dht .validRTPeer (p )
454465 if err != nil {
455466 logger .Errorw ("failed to validate if peer is a DHT peer" , "peer" , p , "error" , err )
456467 } else if b {
457- _ , err := dht .routingTable .TryAddPeer (p , queryPeer )
468+ newlyAdded , err := dht .routingTable .TryAddPeer (p , queryPeer )
458469 if err != nil {
459470 // peer not added.
460471 return
461472 }
462473
463- // If we discovered the peer because of a query, we need to ensure we override the "zero" lastSuccessfulOutboundQuery
474+ // If we freshly added the peer because of a query, we need to ensure we override the "zero" lastUsefulAt
464475 // value that must have been set in the Routing Table for this peer when it was first added during a connection.
465- if queryPeer {
466- dht .routingTable .UpdateLastSuccessfulOutboundQuery (p , time .Now ())
476+ if newlyAdded && queryPeer {
477+ dht .routingTable .UpdateLastUsefulAt (p , time .Now ())
478+ } else if queryPeer {
479+ // the peer is already in our RT, but we just successfully queried it and so let's give it a
480+ // bump on the query time so we don't ping it too soon for a liveliness check.
481+ dht .routingTable .UpdateLastSuccessfulOutboundQueryAt (p , time .Now ())
467482 }
468483 }
469484}
0 commit comments