From 3f48f468acef1f5e44918b0ec37702095398d9bf Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Tue, 4 Feb 2020 20:05:16 +0530 Subject: [PATCH 1/3] replace lru cache blacklist implementation with a time cache --- blacklist.go | 29 +++++++++++++---------------- blacklist_test.go | 5 ++--- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/blacklist.go b/blacklist.go index ba747d81..efc1420c 100644 --- a/blacklist.go +++ b/blacklist.go @@ -1,8 +1,10 @@ package pubsub import ( - lru "github.com/hashicorp/golang-lru" + "time" + "github.com/libp2p/go-libp2p-core/peer" + "github.com/whyrusleeping/timecache" ) // Blacklist is an interface for peer blacklisting. @@ -28,26 +30,21 @@ func (b MapBlacklist) Contains(p peer.ID) bool { return ok } -// LRUBlacklist is a blacklist implementation using an LRU cache -type LRUBlacklist struct { - lru *lru.Cache +// TimeCachedBlacklist is a blacklist implementation using a time cache +type TimeCachedBlacklist struct { + tc *timecache.TimeCache } -// NewLRUBlacklist creates a new LRUBlacklist with capacity cap -func NewLRUBlacklist(cap int) (Blacklist, error) { - c, err := lru.New(cap) - if err != nil { - return nil, err - } - - b := &LRUBlacklist{lru: c} +// NewTimeCachedBlacklist creates a new TimeCachedBlacklist with the given expiry duration +func NewTimeCachedBlacklist(expiry time.Duration) (Blacklist, error) { + b := &TimeCachedBlacklist{timecache.NewTimeCache(expiry)} return b, nil } -func (b LRUBlacklist) Add(p peer.ID) { - b.lru.Add(p, nil) +func (b TimeCachedBlacklist) Add(p peer.ID) { + b.tc.Add(p.String()) } -func (b LRUBlacklist) Contains(p peer.ID) bool { - return b.lru.Contains(p) +func (b TimeCachedBlacklist) Contains(p peer.ID) bool { + return b.tc.Has(p.String()) } diff --git a/blacklist_test.go b/blacklist_test.go index b263b3ea..5b0bcc9f 100644 --- a/blacklist_test.go +++ b/blacklist_test.go @@ -20,8 +20,8 @@ func TestMapBlacklist(t *testing.T) { } -func TestLRUBlacklist(t *testing.T) { - b, err := NewLRUBlacklist(10) +func TestTimeCachedBlacklist(t *testing.T) { + b, err := NewTimeCachedBlacklist(10 * time.Minute) if err != nil { t.Fatal(err) } @@ -32,7 +32,6 @@ func TestLRUBlacklist(t *testing.T) { if !b.Contains(p) { t.Fatal("peer not in the blacklist") } - } func TestBlacklist(t *testing.T) { From 677ca1025be681fcf9732ab5a0f456970c0a8fb8 Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Wed, 5 Feb 2020 14:13:21 +0530 Subject: [PATCH 2/3] added mutex to timecache --- blacklist.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/blacklist.go b/blacklist.go index efc1420c..909a1ee9 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()) } From 6eeed58a50b5e2a62ccbb6bf299ec9efc3bf2955 Mon Sep 17 00:00:00 2001 From: Aarsh Shah Date: Wed, 5 Feb 2020 14:33:20 +0530 Subject: [PATCH 3/3] defer unlocking --- blacklist.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blacklist.go b/blacklist.go index 909a1ee9..c8784b27 100644 --- a/blacklist.go +++ b/blacklist.go @@ -45,8 +45,9 @@ func NewTimeCachedBlacklist(expiry time.Duration) (Blacklist, error) { func (b *TimeCachedBlacklist) Add(p peer.ID) { b.Lock() + defer b.Unlock() + b.tc.Add(p.String()) - b.Unlock() } func (b *TimeCachedBlacklist) Contains(p peer.ID) bool {