Browse Source

don't eagerly update the observed address set in Addrs

allocates and eats a lot of cpu time under the lock;
let a background worker gc periodically.
pull/585/head
vyzo 6 years ago
parent
commit
e76c8a9c38
  1. 26
      p2p/protocol/identify/obsaddr.go

26
p2p/protocol/identify/obsaddr.go

@ -70,21 +70,11 @@ func (oas *ObservedAddrSet) AddrsFor(addr ma.Multiaddr) (addrs []ma.Multiaddr) {
}
now := time.Now()
filteredAddrs := make([]*ObservedAddr, 0, len(observedAddrs))
for _, a := range observedAddrs {
// leave only alive observed addresses
if now.Sub(a.LastSeen) <= oas.ttl {
filteredAddrs = append(filteredAddrs, a)
if a.activated(oas.ttl) {
addrs = append(addrs, a.Addr)
}
if now.Sub(a.LastSeen) <= oas.ttl && a.activated(oas.ttl) {
addrs = append(addrs, a.Addr)
}
}
if len(filteredAddrs) > 0 {
oas.addrs[key] = filteredAddrs
} else {
delete(oas.addrs, key)
}
return addrs
}
@ -100,18 +90,12 @@ func (oas *ObservedAddrSet) Addrs() (addrs []ma.Multiaddr) {
}
now := time.Now()
for local, observedAddrs := range oas.addrs {
filteredAddrs := make([]*ObservedAddr, 0, len(observedAddrs))
for _, observedAddrs := range oas.addrs {
for _, a := range observedAddrs {
// leave only alive observed addresses
if now.Sub(a.LastSeen) <= oas.ttl {
filteredAddrs = append(filteredAddrs, a)
if a.activated(oas.ttl) {
addrs = append(addrs, a.Addr)
}
if now.Sub(a.LastSeen) <= oas.ttl && a.activated(oas.ttl) {
addrs = append(addrs, a.Addr)
}
}
oas.addrs[local] = filteredAddrs
}
return addrs
}

Loading…
Cancel
Save