switch to a bounded cache

This commit is contained in:
whyrusleeping 2019-10-09 21:23:45 +09:00
parent da08ab9fd8
commit b333247fab
2 changed files with 11 additions and 5 deletions

View File

@ -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
View File

@ -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