-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filter observed addresses #917
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package identify | |
|
||
import ( | ||
"context" | ||
"sort" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -30,6 +31,10 @@ var GCInterval = 10 * time.Minute | |
// for adding to an ObservedAddrManager. | ||
var observedAddrManagerWorkerChannelSize = 16 | ||
|
||
// maxObservedAddrsPerIPAndTransport is the maximum number of observed addresses | ||
// we will return for each (IPx/TCP or UDP) group. | ||
var maxObservedAddrsPerIPAndTransport = 2 | ||
|
||
type observation struct { | ||
seenTime time.Time | ||
connDirection network.Direction | ||
|
@@ -46,13 +51,36 @@ type ObservedAddr struct { | |
LastSeen time.Time | ||
} | ||
|
||
func (oa *ObservedAddr) activated(ttl time.Duration) bool { | ||
func (oa *ObservedAddr) activated() bool { | ||
|
||
// We only activate if other peers observed the same address | ||
// of ours at least 4 times. SeenBy peers are removed by GC if | ||
// they say the address more than ttl*ActivationThresh | ||
return len(oa.SeenBy) >= ActivationThresh | ||
} | ||
|
||
func (oa *ObservedAddr) numInbound() int { | ||
count := 0 | ||
for obs := range oa.SeenBy { | ||
if oa.SeenBy[obs].connDirection == network.DirInbound { | ||
count++ | ||
} | ||
} | ||
|
||
return count | ||
} | ||
|
||
func (oa *ObservedAddr) GroupKey() string { | ||
key := "" | ||
protos := oa.Addr.Protocols() | ||
|
||
for i := range protos { | ||
key = key + protos[i].Name | ||
} | ||
|
||
return key | ||
} | ||
Stebalien marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type newObservation struct { | ||
conn network.Conn | ||
observed ma.Multiaddr | ||
|
@@ -111,33 +139,65 @@ func (oas *ObservedAddrManager) AddrsFor(addr ma.Multiaddr) (addrs []ma.Multiadd | |
return | ||
} | ||
|
||
now := time.Now() | ||
for _, a := range observedAddrs { | ||
if now.Sub(a.LastSeen) <= oas.ttl && a.activated(oas.ttl) { | ||
addrs = append(addrs, a.Addr) | ||
} | ||
} | ||
|
||
return addrs | ||
return oas.filter(observedAddrs) | ||
} | ||
|
||
// Addrs return all activated observed addresses | ||
func (oas *ObservedAddrManager) Addrs() (addrs []ma.Multiaddr) { | ||
func (oas *ObservedAddrManager) Addrs() []ma.Multiaddr { | ||
oas.mu.RLock() | ||
defer oas.mu.RUnlock() | ||
|
||
if len(oas.addrs) == 0 { | ||
return nil | ||
} | ||
|
||
var allObserved []*ObservedAddr | ||
for k := range oas.addrs { | ||
allObserved = append(allObserved, oas.addrs[k]...) | ||
} | ||
return oas.filter(allObserved) | ||
} | ||
|
||
func (oas *ObservedAddrManager) filter(observedAddrs []*ObservedAddr) []ma.Multiaddr { | ||
pmap := make(map[string][]*ObservedAddr) | ||
now := time.Now() | ||
for _, observedAddrs := range oas.addrs { | ||
for _, a := range observedAddrs { | ||
if now.Sub(a.LastSeen) <= oas.ttl && a.activated(oas.ttl) { | ||
addrs = append(addrs, a.Addr) | ||
|
||
for i := range observedAddrs { | ||
a := observedAddrs[i] | ||
if now.Sub(a.LastSeen) <= oas.ttl && a.activated() { | ||
// group addresses by their IPX/Transport Protocol(TCP or UDP) pattern. | ||
pat := a.GroupKey() | ||
if len(pat) != 0 { | ||
pmap[pat] = append(pmap[pat], a) | ||
} else { | ||
log.Debugw("unable to group observed addr into IPx/(TCP or UDP) patterm", "address", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this possible? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Umm... it can "theoretically" happen if the address dosen't have any protocols but it probably wouldn't make it so far if that were that case. Removing it to reduce clutter. |
||
a.Addr.String()) | ||
} | ||
} | ||
} | ||
|
||
addrs := make([]ma.Multiaddr, 0, len(observedAddrs)) | ||
for pat := range pmap { | ||
s := pmap[pat] | ||
|
||
// We prefer inbound connection observations over outbound. | ||
// For ties, we prefer the ones with more votes. | ||
sort.Slice(s, func(i int, j int) bool { | ||
first := s[i] | ||
second := s[j] | ||
|
||
if first.numInbound() > second.numInbound() { | ||
return true | ||
} | ||
|
||
return len(first.SeenBy) > len(second.SeenBy) | ||
}) | ||
|
||
for i := 0; i < maxObservedAddrsPerIPAndTransport && i < len(s); i++ { | ||
addrs = append(addrs, s[i].Addr) | ||
} | ||
} | ||
|
||
return addrs | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine but kind of sloppy. We should either use a separator (
/ip4/udp/quic
) or concatenateprotocol.VCode
s (these are prefix-free codes so we don't need separators).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a separator now.