added mutex to timecache
This commit is contained in:
parent
3f48f468ac
commit
677ca1025b
13
blacklist.go
13
blacklist.go
@ -1,6 +1,7 @@
|
|||||||
package pubsub
|
package pubsub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p-core/peer"
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
@ -32,19 +33,25 @@ func (b MapBlacklist) Contains(p peer.ID) bool {
|
|||||||
|
|
||||||
// TimeCachedBlacklist is a blacklist implementation using a time cache
|
// TimeCachedBlacklist is a blacklist implementation using a time cache
|
||||||
type TimeCachedBlacklist struct {
|
type TimeCachedBlacklist struct {
|
||||||
|
sync.RWMutex
|
||||||
tc *timecache.TimeCache
|
tc *timecache.TimeCache
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewTimeCachedBlacklist creates a new TimeCachedBlacklist with the given expiry duration
|
// NewTimeCachedBlacklist creates a new TimeCachedBlacklist with the given expiry duration
|
||||||
func NewTimeCachedBlacklist(expiry time.Duration) (Blacklist, error) {
|
func NewTimeCachedBlacklist(expiry time.Duration) (Blacklist, error) {
|
||||||
b := &TimeCachedBlacklist{timecache.NewTimeCache(expiry)}
|
b := &TimeCachedBlacklist{tc: timecache.NewTimeCache(expiry)}
|
||||||
return b, nil
|
return b, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b TimeCachedBlacklist) Add(p peer.ID) {
|
func (b *TimeCachedBlacklist) Add(p peer.ID) {
|
||||||
|
b.Lock()
|
||||||
b.tc.Add(p.String())
|
b.tc.Add(p.String())
|
||||||
|
b.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b TimeCachedBlacklist) Contains(p peer.ID) bool {
|
func (b *TimeCachedBlacklist) Contains(p peer.ID) bool {
|
||||||
|
b.RLock()
|
||||||
|
defer b.RUnlock()
|
||||||
|
|
||||||
return b.tc.Has(p.String())
|
return b.tc.Has(p.String())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user