use []expiringAddr instead map[string]expiringAddr in addr_manager.go

This commit is contained in:
Arceliar 2018-01-16 17:36:29 -06:00
parent 070490797c
commit 6eedcc1da4

View File

@ -52,13 +52,13 @@ func (e *expiringAddr) ExpiredBy(t time.Time) bool {
return t.After(e.Expires) return t.After(e.Expires)
} }
type addrSet map[string]expiringAddr type addrSlice []expiringAddr
// AddrManager manages addresses. // AddrManager manages addresses.
// The zero-value is ready to be used. // The zero-value is ready to be used.
type AddrManager struct { type AddrManager struct {
addrmu sync.Mutex // guards addrs addrmu sync.Mutex // guards addrs
addrs map[peer.ID]addrSet addrs map[peer.ID]addrSlice
addrSubs map[peer.ID][]*addrSub addrSubs map[peer.ID][]*addrSub
} }
@ -67,7 +67,7 @@ type AddrManager struct {
// So we can use the zero value. // So we can use the zero value.
func (mgr *AddrManager) init() { func (mgr *AddrManager) init() {
if mgr.addrs == nil { if mgr.addrs == nil {
mgr.addrs = make(map[peer.ID]addrSet) mgr.addrs = make(map[peer.ID]addrSlice)
} }
if mgr.addrSubs == nil { if mgr.addrSubs == nil {
mgr.addrSubs = make(map[peer.ID][]*addrSub) mgr.addrSubs = make(map[peer.ID][]*addrSub)
@ -108,10 +108,9 @@ func (mgr *AddrManager) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
// so zero value can be used // so zero value can be used
mgr.init() mgr.init()
amap, found := mgr.addrs[p] amap := make(map[string]expiringAddr)
if !found { for _, ea := range mgr.addrs[p] {
amap = make(addrSet) amap[string(ea.Addr.Bytes())] = ea
mgr.addrs[p] = amap
} }
subs := mgr.addrSubs[p] subs := mgr.addrSubs[p]
@ -134,6 +133,11 @@ func (mgr *AddrManager) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
} }
} }
} }
newAddrs := make([]expiringAddr, 0, len(amap))
for _, ea := range amap {
newAddrs = append(newAddrs, ea)
}
mgr.addrs[p] = newAddrs
} }
// SetAddr calls mgr.SetAddrs(p, addr, ttl) // SetAddr calls mgr.SetAddrs(p, addr, ttl)
@ -150,10 +154,9 @@ func (mgr *AddrManager) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
// so zero value can be used // so zero value can be used
mgr.init() mgr.init()
amap, found := mgr.addrs[p] amap := make(map[string]expiringAddr)
if !found { for _, ea := range mgr.addrs[p] {
amap = make(addrSet) amap[string(ea.Addr.Bytes())] = ea
mgr.addrs[p] = amap
} }
subs := mgr.addrSubs[p] subs := mgr.addrSubs[p]
@ -177,6 +180,11 @@ func (mgr *AddrManager) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
delete(amap, addrs) delete(amap, addrs)
} }
} }
newAddrs := make([]expiringAddr, 0, len(amap))
for _, ea := range amap {
newAddrs = append(newAddrs, ea)
}
mgr.addrs[p] = newAddrs
} }
// UpdateAddrs updates the addresses associated with the given peer that have // UpdateAddrs updates the addresses associated with the given peer that have
@ -221,21 +229,20 @@ func (mgr *AddrManager) Addrs(p peer.ID) []ma.Multiaddr {
now := time.Now() now := time.Now()
good := make([]ma.Multiaddr, 0, len(maddrs)) good := make([]ma.Multiaddr, 0, len(maddrs))
var expired []string cleaned := make([]expiringAddr, 0, len(maddrs))
for s, m := range maddrs { for _, m := range maddrs {
if m.ExpiredBy(now) { if !m.ExpiredBy(now) {
expired = append(expired, s) cleaned = append(cleaned, m)
} else {
good = append(good, m.Addr) good = append(good, m.Addr)
} }
} }
// clean up the expired ones. // clean up the expired ones.
for _, s := range expired { mgr.addrs[p] = cleaned
delete(maddrs, s) if len(cleaned) == 0 {
}
if len(maddrs) == 0 {
delete(mgr.addrs, p) delete(mgr.addrs, p)
} else {
mgr.addrs[p] = cleaned
} }
return good return good
} }
@ -295,9 +302,9 @@ func (mgr *AddrManager) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Mul
mgr.addrSubs[p] = append(mgr.addrSubs[p], sub) mgr.addrSubs[p] = append(mgr.addrSubs[p], sub)
baseaddrset := mgr.addrs[p] baseaddrslice := mgr.addrs[p]
initial := make([]ma.Multiaddr, 0, len(baseaddrset)) initial := make([]ma.Multiaddr, 0, len(baseaddrslice))
for _, a := range baseaddrset { for _, a := range baseaddrslice {
initial = append(initial, a.Addr) initial = append(initial, a.Addr)
} }