Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
fix multiple TTL bugs
Browse files Browse the repository at this point in the history
The first fix independently extends the address expiration time and the address
TTL:

By example:

* We have an address with a TTL of 4s that will expire in 1s.
* We update it with a TTL of 3s.

Before this change:

* We end up with an address with a TTL of 3s that will expire in 3s.

After this change:

* We end up with an address with a TTL of 4s that will expire in 3s.

---

The second fix prevents the in-memory addressbook from announcing existing
addresses every time their TTLs get updated.

---

The third fix correctly updates TTLs for existing addresses in the on-disk
addressbook.

This fixes libp2p/go-libp2p-identify#2
  • Loading branch information
Stebalien committed Jul 24, 2019
1 parent ad0faef commit 86be435
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
18 changes: 14 additions & 4 deletions pstoreds/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,21 @@ Outer:
for _, have := range pr.Addrs {
if incoming.Equal(have.Addr) {
existed[i] = true
if mode == ttlExtend && have.Expiry > newExp {
// if we're only extending TTLs but the addr already has a longer one, we skip it.
continue Outer
switch mode {
case ttlOverride:
have.Ttl = int64(ttl)
case ttlExtend:
if int64(ttl) > have.Ttl {
have.Ttl = int64(ttl)
}
default:
panic("BUG: unimplemented ttl mode")
}
have.Expiry = newExp

if newExp > have.Expiry {
have.Expiry = newExp
}

// we found the address, and addresses cannot be duplicate,
// so let's move on to the next.
continue Outer
Expand Down
15 changes: 12 additions & 3 deletions pstoremem/addr_book.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func (mab *memoryAddrBook) AddAddr(p peer.ID, addr ma.Multiaddr, ttl time.Durati

// AddAddrs gives memoryAddrBook addresses to use, with a given ttl
// (time-to-live), after which the address is no longer valid.
// If the manager has a longer TTL, the operation is a no-op for that address
// This function never reduces the TTL or expiration of an address.
func (mab *memoryAddrBook) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
// if ttl is zero, exit. nothing to do.
if ttl <= 0 {
Expand All @@ -156,10 +156,19 @@ func (mab *memoryAddrBook) AddAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
}
asBytes := addr.Bytes()
a, found := amap[string(asBytes)] // won't allocate.
if !found || exp.After(a.Expires) {
if !found {
// not found, save and announce it.
amap[string(asBytes)] = &expiringAddr{Addr: addr, Expires: exp, TTL: ttl}

mab.subManager.BroadcastAddr(p, addr)
} else {
// Update expiration/TTL independently.
// We never want to reduce either.
if ttl > a.TTL {
a.TTL = ttl
}
if exp.After(a.Expires) {
a.Expires = exp
}
}
}
}
Expand Down

0 comments on commit 86be435

Please sign in to comment.