/
node.go
76 lines (61 loc) · 1.97 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package dht
import (
"github.com/hlandau/dht/krpc"
"github.com/hlandau/goutils/clock"
"net"
"time"
)
// Represents a known node in the DHT.
type node struct {
Addr net.UDPAddr // The address of the peer.
NodeID NodeID // May be invalid if not yet known.
// Outgoing queries for which we are awaiting a response.
PendingQueries map[string]*krpc.Message
// Time of last incoming message from this peer.
LastRxTime time.Time
// Used to determine which infohashes have already been requested from this node.
PastQueries map[InfoHash]time.Time
}
func newNode(addr net.UDPAddr, nodeID NodeID) *node {
return &node{
Addr: addr,
NodeID: nodeID,
PendingQueries: make(map[string]*krpc.Message),
PastQueries: make(map[InfoHash]time.Time),
}
}
func (p *node) IsReachable() bool {
return !p.LastRxTime.IsZero()
}
func (p *node) NumPendingQueries() int {
return len(p.PendingQueries)
}
// Returns true iff the node is due for expiry because of unanswered queries or
// because it has not been heard from.
func (n *node) IsExpired(cleanupPeriod time.Duration) bool {
if !n.IsReachable() && n.NumPendingQueries() > 2 {
return true
}
timeSince := time.Since(n.LastRxTime)
if timeSince > (2*cleanupPeriod + 1*time.Minute) {
return true
}
return false
}
// Returns true iff the node is due for ping. It is assumed this function will
// only be called after checking that IsExpired() is false.
func (n *node) NeedsPing(cleanupPeriod time.Duration) bool {
if !n.IsReachable() || n.NumPendingQueries() == 0 {
return true
}
timeSince := time.Since(n.LastRxTime)
return timeSince >= cleanupPeriod/2
}
// Returns true if a node was contacted recently in relation to some infohash.
func (n *node) WasContactedRecently(infoHash InfoHash, searchRetryPeriod time.Duration) bool {
t, ok := n.PastQueries[infoHash]
return ok && time.Since(t) < searchRetryPeriod
}
func (n *node) MarkContacted(c clock.Clock, infoHash InfoHash) {
n.PastQueries[infoHash] = c.Now()
}