Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace LRU cache blacklist implementation with a time cache #258

Merged
merged 3 commits into from
Feb 5, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions blacklist.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package pubsub

import (
lru "github.com/hashicorp/golang-lru"
"sync"
"time"

"github.com/libp2p/go-libp2p-core/peer"
"github.com/whyrusleeping/timecache"
)

// Blacklist is an interface for peer blacklisting.
Expand All @@ -28,26 +31,27 @@ 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 {
sync.RWMutex
tc *timecache.TimeCache
vyzo marked this conversation as resolved.
Show resolved Hide resolved
}

// 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{tc: 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.Lock()
b.tc.Add(p.String())
vyzo marked this conversation as resolved.
Show resolved Hide resolved
b.Unlock()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defer this, it's more idiomatic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}

func (b LRUBlacklist) Contains(p peer.ID) bool {
return b.lru.Contains(p)
func (b *TimeCachedBlacklist) Contains(p peer.ID) bool {
b.RLock()
defer b.RUnlock()

return b.tc.Has(p.String())
vyzo marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 2 additions & 3 deletions blacklist_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down