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
  • Loading branch information
lazynina committed Nov 1, 2024
1 parent 4ca35f0 commit 3703555
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
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"

Check failure on line 12 in lib/peer.go

View workflow job for this annotation

GitHub Actions / go-test-postgres

missing go.sum entry for module providing package github.com/hashicorp/golang-lru/v2 (imported by github.com/deso-protocol/core/lib); to add:

"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 3703555

Please sign in to comment.