mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2025-01-01 00:20:11 +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
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
ticker *time.Ticker
|
ticker *time.Ticker
|
||||||
ds ds.Datastore
|
ds ds.Batching
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTTLManager(parent context.Context, d ds.Datastore, tick time.Duration) *ttlmanager {
|
func newTTLManager(parent context.Context, d ds.Datastore, tick time.Duration) *ttlmanager {
|
||||||
@ -265,14 +265,23 @@ func (mgr *ttlmanager) tick() {
|
|||||||
defer mgr.RUnlock()
|
defer mgr.RUnlock()
|
||||||
|
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
batch, err := mgr.ds.Batch()
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
for key, entry := range mgr.entries {
|
for key, entry := range mgr.entries {
|
||||||
if entry.ExpiresAt.Before(now) {
|
if entry.ExpiresAt.Before(now) {
|
||||||
if err := mgr.ds.Delete(key); err != nil {
|
if err := batch.Delete(key); err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
delete(mgr.entries, key)
|
delete(mgr.entries, key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
err := batch.Commit()
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mgr *ttlmanager) setTTLs(keys []ds.Key, ttl time.Duration) {
|
func (mgr *ttlmanager) setTTLs(keys []ds.Key, ttl time.Duration) {
|
||||||
|
Loading…
Reference in New Issue
Block a user