Skip to content

Commit

Permalink
FIXUP make lookupCheck and peerRecentlyQueried methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Mar 7, 2023
1 parent be25008 commit f67dfa9
Showing 1 changed file with 30 additions and 30 deletions.
60 changes: 30 additions & 30 deletions dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,11 @@ type IpfsDHT struct {

// A function performing a lookup request to a remote peer.ID, verifying that it is able to
// answer it correctly
lookupCheck func(context.Context, peer.ID) error
lookupCheckTimeout time.Duration
lookupCheckInterval time.Duration // time interval during which we don't try to query the same peer again
// recentlyCheckedPeers contains the peers recently queried with the time at which they were queried
recentlyCheckedPeers map[peer.ID]time.Time
recentlyCheckedPeersLk sync.Mutex
peerRecentlyQueried func(peer.ID) bool

// A function returning a set of bootstrap peers to fallback on if all other attempts to fix
// the routing table fail (or, e.g., this is the first time this node is
Expand Down Expand Up @@ -327,36 +325,8 @@ func makeDHT(ctx context.Context, h host.Host, cfg dhtcfg.Config) (*IpfsDHT, err
dht.routingTable = rt
dht.bootstrapPeers = cfg.BootstrapPeers

dht.lookupCheck = func(ctx context.Context, p peer.ID) error {
// lookup request to p requesting for its own peer.ID
peerids, err := dht.protoMessenger.GetClosestPeers(ctx, p, p)
// p should return at least its own peerid
if err == nil && len(peerids) == 0 {
return fmt.Errorf("peer %s failed to return its closest peers, got %d", p, len(peerids))
}
return err
}
dht.lookupCheckTimeout = cfg.RoutingTable.RefreshQueryTimeout
dht.recentlyCheckedPeers = make(map[peer.ID]time.Time)
dht.peerRecentlyQueried = func(p peer.ID) bool {
dht.recentlyCheckedPeersLk.Lock()

now := time.Now()

// clean recentlyCheckedPeers
for peerid, t := range dht.recentlyCheckedPeers {
// remove peers that have been queried more than lookupCheckInterval ago
if t.Add(dht.lookupCheckInterval).Before(now) {
delete(dht.recentlyCheckedPeers, peerid)
}
}

// if p still in recentlyCheckedPeers, it has been queried less than
// lookupCheckInterval ago
_, ok := dht.recentlyCheckedPeers[p]
dht.recentlyCheckedPeersLk.Unlock()
return ok
}

// rt refresh manager
rtRefresh, err := makeRtRefreshManager(dht, cfg, maxLastSuccessfulOutboundThreshold)
Expand Down Expand Up @@ -389,6 +359,36 @@ func makeDHT(ctx context.Context, h host.Host, cfg dhtcfg.Config) (*IpfsDHT, err
return dht, nil
}

func (dht *IpfsDHT) lookupCheck(ctx context.Context, p peer.ID) error {
// lookup request to p requesting for its own peer.ID
peerids, err := dht.protoMessenger.GetClosestPeers(ctx, p, p)
// p should return at least its own peerid
if err == nil && len(peerids) == 0 {
return fmt.Errorf("peer %s failed to return its closest peers, got %d", p, len(peerids))
}
return err
}

func (dht *IpfsDHT) peerRecentlyQueried(p peer.ID) bool {
dht.recentlyCheckedPeersLk.Lock()
defer dht.recentlyCheckedPeersLk.Unlock()

now := time.Now()

// clean recentlyCheckedPeers
for peerid, t := range dht.recentlyCheckedPeers {
// remove peers that have been queried more than lookupCheckInterval ago
if t.Add(dht.lookupCheckInterval).Before(now) {
delete(dht.recentlyCheckedPeers, peerid)
}
}

// if p still in recentlyCheckedPeers, it has been queried less than
// lookupCheckInterval ago
_, ok := dht.recentlyCheckedPeers[p]
return ok
}

func makeRtRefreshManager(dht *IpfsDHT, cfg dhtcfg.Config, maxLastSuccessfulOutboundThreshold time.Duration) (*rtrefresh.RtRefreshManager, error) {
keyGenFnc := func(cpl uint) (string, error) {
p, err := dht.routingTable.GenRandPeerID(cpl)
Expand Down

0 comments on commit f67dfa9

Please sign in to comment.