@@ -11,19 +11,32 @@ import (
11
11
ma "github.com/multiformats/go-multiaddr"
12
12
)
13
13
14
- const ActivationThresh = 4
15
-
14
+ // ActivationThresh sets how many times an address must be seen as "activated"
15
+ // and therefore advertised to other peers as an address that the local peer
16
+ // can be contacted on. The "seen" events expire by default after 40 minutes
17
+ // (OwnObservedAddressTTL * ActivationThreshold). The are cleaned up during
18
+ // the GC rounds set by GCInterval.
19
+ var ActivationThresh = 4
20
+
21
+ // GCInterval specicies how often to make a round cleaning seen events and
22
+ // observed addresses. An address will be cleaned if it has not been seen in
23
+ // OwnObservedAddressTTL (10 minutes). A "seen" event will be cleaned up if
24
+ // it is older than OwnObservedAddressTTL * ActivationThresh (40 minutes).
16
25
var GCInterval = 10 * time .Minute
17
26
27
+ // observedAddrSetWorkerChannelSize defines how many addresses can be enqueued
28
+ // for adding to an ObservedAddrSet.
29
+ var observedAddrSetWorkerChannelSize = 16
30
+
18
31
type observation struct {
19
32
seenTime time.Time
20
33
connDirection network.Direction
21
34
}
22
35
23
36
// ObservedAddr is an entry for an address reported by our peers.
24
37
// We only use addresses that:
25
- // - have been observed at least 4 times in last 1h . (counter symmetric nats)
26
- // - have been observed at least once recently (1h ), because our position in the
38
+ // - have been observed at least 4 times in last 40 minutes . (counter symmetric nats)
39
+ // - have been observed at least once recently (10 minutes ), because our position in the
27
40
// network, or network port mapppings, may have changed.
28
41
type ObservedAddr struct {
29
42
Addr ma.Multiaddr
@@ -32,8 +45,9 @@ type ObservedAddr struct {
32
45
}
33
46
34
47
func (oa * ObservedAddr ) activated (ttl time.Duration ) bool {
35
- // We only activate if in the TTL other peers observed the same address
36
- // of ours at least 4 times.
48
+ // We only activate if other peers observed the same address
49
+ // of ours at least 4 times. SeenBy peers are removed by GC if
50
+ // they say the address more than ttl*ActivationThresh
37
51
return len (oa .SeenBy ) >= ActivationThresh
38
52
}
39
53
@@ -55,11 +69,13 @@ type ObservedAddrSet struct {
55
69
wch chan newObservation
56
70
}
57
71
72
+ // NewObservedAddrSet returns a new set using peerstore.OwnObservedAddressTTL
73
+ // as the TTL.
58
74
func NewObservedAddrSet (ctx context.Context ) * ObservedAddrSet {
59
75
oas := & ObservedAddrSet {
60
76
addrs : make (map [string ][]* ObservedAddr ),
61
77
ttl : peerstore .OwnObservedAddrTTL ,
62
- wch : make (chan newObservation , 16 ),
78
+ wch : make (chan newObservation , observedAddrSetWorkerChannelSize ),
63
79
}
64
80
go oas .worker (ctx )
65
81
return oas
@@ -111,6 +127,7 @@ func (oas *ObservedAddrSet) Addrs() (addrs []ma.Multiaddr) {
111
127
return addrs
112
128
}
113
129
130
+ // Add attemps to queue a new observed address to be added to the set.
114
131
func (oas * ObservedAddrSet ) Add (observed , local , observer ma.Multiaddr ,
115
132
direction network.Direction ) {
116
133
select {
@@ -150,7 +167,7 @@ func (oas *ObservedAddrSet) gc() {
150
167
for _ , a := range observedAddrs {
151
168
// clean up SeenBy set
152
169
for k , ob := range a .SeenBy {
153
- if now .Sub (ob .seenTime ) > oas .ttl * ActivationThresh {
170
+ if now .Sub (ob .seenTime ) > oas .ttl * time . Duration ( ActivationThresh ) {
154
171
delete (a .SeenBy , k )
155
172
}
156
173
}
@@ -217,12 +234,14 @@ func observerGroup(m ma.Multiaddr) string {
217
234
return string (first .Bytes ())
218
235
}
219
236
237
+ // SetTTL sets the TTL of an observed address-set.
220
238
func (oas * ObservedAddrSet ) SetTTL (ttl time.Duration ) {
221
239
oas .Lock ()
222
240
defer oas .Unlock ()
223
241
oas .ttl = ttl
224
242
}
225
243
244
+ // TTL gets the TTL of an observed address-set.
226
245
func (oas * ObservedAddrSet ) TTL () time.Duration {
227
246
oas .RLock ()
228
247
defer oas .RUnlock ()
0 commit comments