mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-01-28 04:50:07 +08:00
Merge pull request #157 from libp2p/fix/delete-addrs-update-zero
fix: delete addrs when "updating" them to zero
This commit is contained in:
commit
b4ad10c61b
@ -25,7 +25,7 @@ type expiringAddr struct {
|
||||
}
|
||||
|
||||
func (e *expiringAddr) ExpiredBy(t time.Time) bool {
|
||||
return t.After(e.Expires)
|
||||
return !t.Before(e.Expires)
|
||||
}
|
||||
|
||||
type peerRecordState struct {
|
||||
@ -315,9 +315,15 @@ func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL t
|
||||
defer s.Unlock()
|
||||
exp := time.Now().Add(newTTL)
|
||||
amap, found := s.addrs[p]
|
||||
if found {
|
||||
for k, a := range amap {
|
||||
if oldTTL == a.TTL {
|
||||
if !found {
|
||||
return
|
||||
}
|
||||
|
||||
for k, a := range amap {
|
||||
if oldTTL == a.TTL {
|
||||
if newTTL == 0 {
|
||||
delete(amap, k)
|
||||
} else {
|
||||
a.TTL = newTTL
|
||||
a.Expires = exp
|
||||
amap[k] = a
|
||||
|
@ -210,6 +210,18 @@ func testUpdateTTLs(m pstore.AddrBook) func(t *testing.T) {
|
||||
m.UpdateAddrs(id, time.Hour, time.Minute)
|
||||
})
|
||||
|
||||
t.Run("update to 0 clears addrs", func(t *testing.T) {
|
||||
id := GeneratePeerIDs(1)[0]
|
||||
addrs := GenerateAddrs(1)
|
||||
|
||||
// Shouldn't panic.
|
||||
m.SetAddrs(id, addrs, time.Hour)
|
||||
m.UpdateAddrs(id, time.Hour, 0)
|
||||
if len(m.Addrs(id)) != 0 {
|
||||
t.Error("expected no addresses")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("update ttls successfully", func(t *testing.T) {
|
||||
ids := GeneratePeerIDs(2)
|
||||
addrs1, addrs2 := GenerateAddrs(2), GenerateAddrs(2)
|
||||
|
Loading…
Reference in New Issue
Block a user