switch to a bounded cache
This commit is contained in:
parent
da08ab9fd8
commit
b333247fab
@ -3,29 +3,34 @@ package chain
|
||||
import (
|
||||
"sync"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
"github.com/ipfs/go-cid"
|
||||
)
|
||||
|
||||
type BadBlockCache struct {
|
||||
lk sync.Mutex
|
||||
badBlocks map[cid.Cid]struct{}
|
||||
badBlocks *lru.ARCCache
|
||||
}
|
||||
|
||||
func NewBadBlockCache() *BadBlockCache {
|
||||
cache, err := lru.NewARC(8192)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &BadBlockCache{
|
||||
badBlocks: make(map[cid.Cid]struct{}),
|
||||
badBlocks: cache,
|
||||
}
|
||||
}
|
||||
|
||||
func (bts *BadBlockCache) Add(c cid.Cid) {
|
||||
bts.lk.Lock()
|
||||
defer bts.lk.Unlock()
|
||||
bts.badBlocks[c] = struct{}{}
|
||||
bts.badBlocks.Add(c, nil)
|
||||
}
|
||||
|
||||
func (bts *BadBlockCache) Has(c cid.Cid) bool {
|
||||
bts.lk.Lock()
|
||||
defer bts.lk.Unlock()
|
||||
_, ok := bts.badBlocks[c]
|
||||
return ok
|
||||
return bts.badBlocks.Contains(c)
|
||||
}
|
||||
|
1
go.mod
1
go.mod
@ -14,6 +14,7 @@ require (
|
||||
github.com/gbrlsnchs/jwt/v3 v3.0.0-beta.1
|
||||
github.com/go-ole/go-ole v1.2.4 // indirect
|
||||
github.com/gorilla/websocket v1.4.0
|
||||
github.com/hashicorp/golang-lru v0.5.3
|
||||
github.com/ipfs/go-bitswap v0.1.8
|
||||
github.com/ipfs/go-block-format v0.0.2
|
||||
github.com/ipfs/go-blockservice v0.1.3-0.20190908200855-f22eea50656c
|
||||
|
Loading…
Reference in New Issue
Block a user