add method to atomically update TTLs

This allows one to find all addresses with a given TTL and update them to have a
new TTL.
This commit is contained in:
Steven Allen 2018-01-04 13:00:37 -08:00
parent 420131a845
commit f6bb5a31c1
2 changed files with 36 additions and 6 deletions

View File

@ -40,12 +40,13 @@ const (
)
type expiringAddr struct {
Addr ma.Multiaddr
TTL time.Time
Addr ma.Multiaddr
TTL time.Duration
Expires time.Time
}
func (e *expiringAddr) ExpiredBy(t time.Time) bool {
return t.After(e.TTL)
return t.After(e.Expires)
}
type addrSet map[string]expiringAddr
@ -122,8 +123,8 @@ func (mgr *AddrManager) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
addrstr := string(addr.Bytes())
a, found := amap[addrstr]
if !found || exp.After(a.TTL) {
amap[addrstr] = expiringAddr{Addr: addr, TTL: exp}
if !found || exp.After(a.Expires) {
amap[addrstr] = expiringAddr{Addr: addr, Expires: exp, TTL: ttl}
for _, sub := range subs {
sub.pubAddr(addr)
@ -164,7 +165,7 @@ func (mgr *AddrManager) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
addrs := string(addr.Bytes())
if ttl > 0 {
amap[addrs] = expiringAddr{Addr: addr, TTL: exp}
amap[addrs] = expiringAddr{Addr: addr, Expires: exp, TTL: ttl}
for _, sub := range subs {
sub.pubAddr(addr)
@ -175,6 +176,31 @@ func (mgr *AddrManager) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Durat
}
}
// UpdateAddrs updates the addresses associated with the given peer that have
// the given oldTTL to have the given newTTL.
func (mgr *AddrManager) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL time.Duration) {
mgr.addrmu.Lock()
defer mgr.addrmu.Unlock()
if mgr.addrs == nil {
return
}
amap, found := mgr.addrs[p]
if !found {
return
}
exp := time.Now().Add(newTTL)
for addrstr, aexp := range amap {
if oldTTL == aexp.TTL {
aexp.TTL = newTTL
aexp.Expires = exp
amap[addrstr] = aexp
}
}
}
// Addresses returns all known (and valid) addresses for a given
func (mgr *AddrManager) Addrs(p peer.ID) []ma.Multiaddr {
mgr.addrmu.Lock()

View File

@ -69,6 +69,10 @@ type AddrBook interface {
// This is used when we receive the best estimate of the validity of an address.
SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration)
// UpdateAddrs updates the addresses associated with the given peer that have
// the given oldTTL to have the given newTTL.
UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL time.Duration)
// Addresses returns all known (and valid) addresses for a given peer
Addrs(p peer.ID) []ma.Multiaddr