Skip to content

Commit

Permalink
replace decred w/ hashicorp lru. only call purge when disconnecting (#…
Browse files Browse the repository at this point in the history
…1428)

* replace decred w/ hashicorp lru. only call purge when disconnecting

* go get golang-lru

* go mod tidy
  • Loading branch information
lazynina authored Nov 1, 2024
1 parent 4ca35f0 commit 13729e8
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/go-pg/pg/v10 v10.13.0
github.com/golang/glog v1.2.2
github.com/google/uuid v1.6.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/mitchellh/go-homedir v1.1.0
github.com/oleiade/lane v1.0.1
github.com/onflow/crypto v0.25.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ github.com/hashicorp/go-secure-stdlib/strutil v0.1.2 h1:kes8mmyCpxJsI7FTwtzRqEy9
github.com/hashicorp/go-secure-stdlib/strutil v0.1.2/go.mod h1:Gou2R9+il93BqX25LAKCLuM+y9U2T4hlwvT1yprcna4=
github.com/hashicorp/go-sockaddr v1.0.7 h1:G+pTkSO01HpR5qCxg7lxfsFEZaG+C0VssTy/9dbT+Fw=
github.com/hashicorp/go-sockaddr v1.0.7/go.mod h1:FZQbEYa1pxkQ7WLpyXJ6cbjpT8q0YgQaK/JakXqGyWw=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.1-vault-5 h1:kI3hhbbyzr4dldA8UdTb7ZlVVlI2DACdCfz31RPDgJM=
github.com/hashicorp/hcl v1.0.1-vault-5/go.mod h1:XYhtn6ijBSAj6n4YqAaf7RBPS4I06AItNorpy+MoQNM=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
Expand Down
15 changes: 8 additions & 7 deletions lib/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"sync/atomic"
"time"

"github.com/decred/dcrd/container/lru"
"github.com/hashicorp/golang-lru/v2"

"github.com/btcsuite/btcd/wire"
"github.com/golang/glog"
Expand Down Expand Up @@ -111,7 +111,7 @@ type Peer struct {

// Inventory stuff.
// The inventory that we know the peer already has.
knownInventory *lru.Set[InvVect]
knownInventory *lru.Cache[InvVect, struct{}]

// Whether the peer is ready to receive INV messages. For a peer that
// still needs a mempool download, this is false.
Expand Down Expand Up @@ -292,7 +292,7 @@ func (pp *Peer) HelpHandleInv(msg *MsgDeSoInv) {

for _, invVect := range msg.InvList {
// No matter what, add the inv to the peer's known inventory.
pp.knownInventory.Put(*invVect)
pp.knownInventory.Add(*invVect, struct{}{})

// If this is a hash we are currently processing, no need to do anything.
// This check serves to fill the gap between the time when we've decided
Expand Down Expand Up @@ -640,6 +640,8 @@ func NewPeer(_id uint64, _conn net.Conn, _isOutbound bool, _netAddr *wire.NetAdd
_syncType NodeSyncType,
peerDisconnectedChan chan *Peer) *Peer {

knownInventoryCache, _ := lru.New[InvVect, struct{}](maxKnownInventory)

pp := Peer{
ID: _id,
cmgr: _cmgr,
Expand All @@ -652,7 +654,7 @@ func NewPeer(_id uint64, _conn net.Conn, _isOutbound bool, _netAddr *wire.NetAdd
outputQueueChan: make(chan DeSoMessage),
peerDisconnectedChan: peerDisconnectedChan,
quit: make(chan interface{}),
knownInventory: lru.NewSet[InvVect](maxKnownInventory),
knownInventory: knownInventoryCache,
blocksToSend: make(map[BlockHash]bool),
stallTimeoutSeconds: _stallTimeoutSeconds,
minTxFeeRateNanosPerKB: _minFeeRateNanosPerKB,
Expand Down Expand Up @@ -978,7 +980,7 @@ out:

// Add the new inventory to the peer's knownInventory.
for _, invVect := range invMsg.InvList {
pp.knownInventory.Put(*invVect)
pp.knownInventory.Add(*invVect, struct{}{})
}
}

Expand Down Expand Up @@ -1366,8 +1368,7 @@ func (pp *Peer) Disconnect(reason string) {
close(pp.quit)

// Free the cache of known inventory.
pp.knownInventory.Clear()
pp.knownInventory = nil
pp.knownInventory.Purge()

// Add the Peer to donePeers so that the ConnectionManager and Server can do any
// cleanup they need to do.
Expand Down
2 changes: 1 addition & 1 deletion lib/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,7 @@ func (srv *Server) _relayTransactions() {
// Add the transaction to the peer's known inventory. We do
// it here when we enqueue the message to the peers outgoing
// message queue so that we don't have remember to do it later.
pp.knownInventory.Put(*invVect)
pp.knownInventory.Add(*invVect, struct{}{})
invMsg.InvList = append(invMsg.InvList, invVect)
}
if len(invMsg.InvList) > 0 {
Expand Down

0 comments on commit 13729e8

Please sign in to comment.