Skip to content

Commit 210dcc6

Browse files
authored
Merge pull request libp2p#740 from libp2p/identify/configurable-threshold
Identify: Make activation threshold configurable
2 parents b8e13da + 7e49c66 commit 210dcc6

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

p2p/protocol/identify/obsaddr.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,32 @@ import (
1111
ma "github.com/multiformats/go-multiaddr"
1212
)
1313

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).
1625
var GCInterval = 10 * time.Minute
1726

27+
// observedAddrSetWorkerChannelSize defines how many addresses can be enqueued
28+
// for adding to an ObservedAddrSet.
29+
var observedAddrSetWorkerChannelSize = 16
30+
1831
type observation struct {
1932
seenTime time.Time
2033
connDirection network.Direction
2134
}
2235

2336
// ObservedAddr is an entry for an address reported by our peers.
2437
// 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
2740
// network, or network port mapppings, may have changed.
2841
type ObservedAddr struct {
2942
Addr ma.Multiaddr
@@ -32,8 +45,9 @@ type ObservedAddr struct {
3245
}
3346

3447
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
3751
return len(oa.SeenBy) >= ActivationThresh
3852
}
3953

@@ -55,11 +69,13 @@ type ObservedAddrSet struct {
5569
wch chan newObservation
5670
}
5771

72+
// NewObservedAddrSet returns a new set using peerstore.OwnObservedAddressTTL
73+
// as the TTL.
5874
func NewObservedAddrSet(ctx context.Context) *ObservedAddrSet {
5975
oas := &ObservedAddrSet{
6076
addrs: make(map[string][]*ObservedAddr),
6177
ttl: peerstore.OwnObservedAddrTTL,
62-
wch: make(chan newObservation, 16),
78+
wch: make(chan newObservation, observedAddrSetWorkerChannelSize),
6379
}
6480
go oas.worker(ctx)
6581
return oas
@@ -111,6 +127,7 @@ func (oas *ObservedAddrSet) Addrs() (addrs []ma.Multiaddr) {
111127
return addrs
112128
}
113129

130+
// Add attemps to queue a new observed address to be added to the set.
114131
func (oas *ObservedAddrSet) Add(observed, local, observer ma.Multiaddr,
115132
direction network.Direction) {
116133
select {
@@ -150,7 +167,7 @@ func (oas *ObservedAddrSet) gc() {
150167
for _, a := range observedAddrs {
151168
// clean up SeenBy set
152169
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) {
154171
delete(a.SeenBy, k)
155172
}
156173
}
@@ -217,12 +234,14 @@ func observerGroup(m ma.Multiaddr) string {
217234
return string(first.Bytes())
218235
}
219236

237+
// SetTTL sets the TTL of an observed address-set.
220238
func (oas *ObservedAddrSet) SetTTL(ttl time.Duration) {
221239
oas.Lock()
222240
defer oas.Unlock()
223241
oas.ttl = ttl
224242
}
225243

244+
// TTL gets the TTL of an observed address-set.
226245
func (oas *ObservedAddrSet) TTL() time.Duration {
227246
oas.RLock()
228247
defer oas.RUnlock()

0 commit comments

Comments
 (0)