mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2024-12-28 23:50:12 +08:00
Update ttlmanager to use ds.Batching
This commit is contained in:
parent
78cd2c5abd
commit
9eeea343d5
@ -231,7 +231,7 @@ type ttlmanager struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
ticker *time.Ticker
|
||||
ds ds.Datastore
|
||||
ds ds.Batching
|
||||
}
|
||||
|
||||
func newTTLManager(parent context.Context, d ds.Datastore, tick time.Duration) *ttlmanager {
|
||||
@ -265,14 +265,23 @@ func (mgr *ttlmanager) tick() {
|
||||
defer mgr.RUnlock()
|
||||
|
||||
now := time.Now()
|
||||
batch, err := mgr.ds.Batch()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
for key, entry := range mgr.entries {
|
||||
if entry.ExpiresAt.Before(now) {
|
||||
if err := mgr.ds.Delete(key); err != nil {
|
||||
if err := batch.Delete(key); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
delete(mgr.entries, key)
|
||||
}
|
||||
}
|
||||
err := batch.Commit()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (mgr *ttlmanager) setTTLs(keys []ds.Key, ttl time.Duration) {
|
||||
|
Loading…
Reference in New Issue
Block a user