mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-03-31 14:00:06 +08:00
Fix logic for AddAddrs
This commit is contained in:
parent
9eeea343d5
commit
e7dca4205e
@ -109,7 +109,7 @@ func (mgr *DatastoreAddrManager) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl t
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Allows us to support AddAddr and SetAddr in one function
|
// Allows us to support AddAddr and SetAddr in one function
|
||||||
if !has || !add {
|
if !has {
|
||||||
if err := batch.Put(key, addr.Bytes()); err != nil {
|
if err := batch.Put(key, addr.Bytes()); err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
@ -119,7 +119,7 @@ func (mgr *DatastoreAddrManager) setAddrs(p peer.ID, addrs []ma.Multiaddr, ttl t
|
|||||||
log.Errorf("failed to write addresses for peer %s: %s\n", p.Pretty(), err)
|
log.Errorf("failed to write addresses for peer %s: %s\n", p.Pretty(), err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
mgr.ttlManager.setTTLs(keys, ttl)
|
mgr.ttlManager.setTTLs(keys, ttl, add)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Errorf("failed to avoid write conflict for peer %s after %d retries\n", p.Pretty(), dsWriteRetries)
|
log.Errorf("failed to avoid write conflict for peer %s after %d retries\n", p.Pretty(), dsWriteRetries)
|
||||||
@ -236,12 +236,16 @@ type ttlmanager struct {
|
|||||||
|
|
||||||
func newTTLManager(parent context.Context, d ds.Datastore, tick time.Duration) *ttlmanager {
|
func newTTLManager(parent context.Context, d ds.Datastore, tick time.Duration) *ttlmanager {
|
||||||
ctx, cancel := context.WithCancel(parent)
|
ctx, cancel := context.WithCancel(parent)
|
||||||
|
batching, ok := d.(ds.Batching)
|
||||||
|
if !ok {
|
||||||
|
panic("must construct ttlmanager with batching datastore")
|
||||||
|
}
|
||||||
mgr := &ttlmanager{
|
mgr := &ttlmanager{
|
||||||
entries: make(map[ds.Key]*ttlentry),
|
entries: make(map[ds.Key]*ttlentry),
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
ticker: time.NewTicker(tick),
|
ticker: time.NewTicker(tick),
|
||||||
ds: d,
|
ds: batching,
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -278,22 +282,32 @@ func (mgr *ttlmanager) tick() {
|
|||||||
delete(mgr.entries, key)
|
delete(mgr.entries, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
err := batch.Commit()
|
err = batch.Commit()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mgr *ttlmanager) setTTLs(keys []ds.Key, ttl time.Duration) {
|
func (mgr *ttlmanager) setTTLs(keys []ds.Key, ttl time.Duration, add bool) {
|
||||||
mgr.Lock()
|
mgr.Lock()
|
||||||
defer mgr.Unlock()
|
defer mgr.Unlock()
|
||||||
|
|
||||||
expiration := time.Now().Add(ttl)
|
expiration := time.Now().Add(ttl)
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
if ttl <= 0 {
|
update := true
|
||||||
delete(mgr.entries, key)
|
if add {
|
||||||
} else {
|
if entry, ok := mgr.entries[key]; ok {
|
||||||
mgr.entries[key] = &ttlentry{TTL: ttl, ExpiresAt: expiration}
|
if entry.ExpiresAt.After(expiration) {
|
||||||
|
update = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if update {
|
||||||
|
if ttl <= 0 {
|
||||||
|
delete(mgr.entries, key)
|
||||||
|
} else {
|
||||||
|
mgr.entries[key] = &ttlentry{TTL: ttl, ExpiresAt: expiration}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user