mirror of
https://github.com/libp2p/go-libp2p-peerstore.git
synced 2024-12-27 23:40:16 +08:00
Revert "use a Pool for address record objects."
This commit is contained in:
parent
dd2a07ea4a
commit
943429ff4b
@ -18,8 +18,8 @@ import (
|
||||
pb "github.com/libp2p/go-libp2p-peerstore/pb"
|
||||
pstoremem "github.com/libp2p/go-libp2p-peerstore/pstoremem"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
ma "github.com/multiformats/go-multiaddr"
|
||||
lru "github.com/raulk/golang-lru"
|
||||
b32 "github.com/whyrusleeping/base32"
|
||||
)
|
||||
|
||||
@ -38,14 +38,6 @@ var (
|
||||
// GC lookahead entries are stored in keys with pattern:
|
||||
// /peers/gc/addrs/<unix timestamp of next visit>/<peer ID b32> => nil
|
||||
gcLookaheadBase = ds.NewKey("/peers/gc/addrs")
|
||||
arPool = &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return &addrsRecord{
|
||||
AddrBookRecord: &pb.AddrBookRecord{},
|
||||
dirty: false,
|
||||
}
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
// addrsRecord decorates the AddrBookRecord with locks and metadata.
|
||||
@ -55,11 +47,6 @@ type addrsRecord struct {
|
||||
dirty bool
|
||||
}
|
||||
|
||||
func (r *addrsRecord) Reset() {
|
||||
r.AddrBookRecord.Reset()
|
||||
r.dirty = false
|
||||
}
|
||||
|
||||
// FlushInTxn writes the record to the datastore by calling ds.Put, unless the record is
|
||||
// marked for deletion, in which case the deletion is executed via ds.Delete.
|
||||
func (r *addrsRecord) FlushInTxn(txn ds.Txn) (err error) {
|
||||
@ -172,11 +159,7 @@ var _ pstore.AddrBook = (*dsAddrBook)(nil)
|
||||
func NewAddrBook(ctx context.Context, store ds.TxnDatastore, opts Options) (ab *dsAddrBook, err error) {
|
||||
var cache cache = new(noopCache)
|
||||
if opts.CacheSize > 0 {
|
||||
evictCallback := func(key interface{}, value interface{}) {
|
||||
value.(*addrsRecord).Reset()
|
||||
arPool.Put(value)
|
||||
}
|
||||
if cache, err = lru.NewARCWithEvict(int(opts.CacheSize), evictCallback); err != nil {
|
||||
if cache, err = lru.NewARC(int(opts.CacheSize)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@ -242,7 +225,7 @@ func (ab *dsAddrBook) loadRecord(id peer.ID, cache bool, update bool) (pr *addrs
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
pr = arPool.Get().(*addrsRecord)
|
||||
pr = &addrsRecord{AddrBookRecord: &pb.AddrBookRecord{}}
|
||||
if err = pr.Unmarshal(data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -250,8 +233,7 @@ func (ab *dsAddrBook) loadRecord(id peer.ID, cache bool, update bool) (pr *addrs
|
||||
ab.asyncFlush(pr)
|
||||
}
|
||||
} else {
|
||||
pr = arPool.Get().(*addrsRecord)
|
||||
pr.Id = &pb.ProtoPeerID{ID: id}
|
||||
pr = &addrsRecord{AddrBookRecord: &pb.AddrBookRecord{Id: &pb.ProtoPeerID{ID: id}}}
|
||||
}
|
||||
|
||||
if cache {
|
||||
@ -321,10 +303,7 @@ func (ab *dsAddrBook) purgeCycle() {
|
||||
}
|
||||
|
||||
var id peer.ID
|
||||
record := arPool.Get().(*addrsRecord)
|
||||
record.Reset()
|
||||
defer arPool.Put(record)
|
||||
|
||||
record := &addrsRecord{AddrBookRecord: &pb.AddrBookRecord{}}
|
||||
txn, err := ab.ds.NewTransaction(false)
|
||||
if err != nil {
|
||||
log.Warningf("failed while purging entries: %v", err)
|
||||
@ -457,9 +436,7 @@ func (ab *dsAddrBook) populateLookahead() {
|
||||
until := time.Now().Add(ab.opts.GCLookaheadInterval).Unix()
|
||||
|
||||
var id peer.ID
|
||||
record := arPool.Get().(*addrsRecord)
|
||||
defer arPool.Put(record)
|
||||
|
||||
record := &addrsRecord{AddrBookRecord: &pb.AddrBookRecord{}}
|
||||
txn, err := ab.ds.NewTransaction(false)
|
||||
if err != nil {
|
||||
log.Warningf("failed while filling lookahead GC region: %v", err)
|
||||
|
Loading…
Reference in New Issue
Block a user