From 677ca1025be681fcf9732ab5a0f456970c0a8fb8 Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Wed, 5 Feb 2020 14:13:21 +0530 Subject: [PATCH] added mutex to timecache --- blacklist.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/blacklist.go b/blacklist.go index efc1420..909a1ee 100644 --- a/blacklist.go +++ b/blacklist.go @@ -1,6 +1,7 @@ package pubsub import ( + "sync" "time" "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 type TimeCachedBlacklist struct { + sync.RWMutex tc *timecache.TimeCache } // NewTimeCachedBlacklist creates a new TimeCachedBlacklist with the given expiry duration func NewTimeCachedBlacklist(expiry time.Duration) (Blacklist, error) { - b := &TimeCachedBlacklist{timecache.NewTimeCache(expiry)} + b := &TimeCachedBlacklist{tc: timecache.NewTimeCache(expiry)} 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.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()) }