Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a cache to store the recent requests and don't ask for the same b… #1679

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions quai/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/event"
"github.com/dominant-strategies/go-quai/log"
lru "github.com/hnlq715/golang-lru"
)

const (
Expand All @@ -20,6 +21,10 @@ const (
c_checkNextPrimeBlockInterval = 60 * time.Second
// c_txsChanSize is the size of channel listening to the new txs event
c_newTxsChanSize = 100
// c_recentBlockReqCache is the size of the cache for the recent block requests
c_recentBlockReqCache = 1000
// c_recentBlockReqTimeout is the timeout for the recent block requests cache
c_recentBlockReqTimeout = 1 * time.Minute
)

// handler manages the fetch requests from the core and tx pool also takes care of the tx broadcast
Expand All @@ -34,6 +39,8 @@ type handler struct {
wg sync.WaitGroup
quitCh chan struct{}
logger *log.Logger

recentBlockReqCache *lru.Cache // cache the latest requests on a 1 min timer
}

func newHandler(p2pBackend NetworkingAPI, core *core.Core, nodeLocation common.Location, logger *log.Logger) *handler {
Expand All @@ -44,6 +51,7 @@ func newHandler(p2pBackend NetworkingAPI, core *core.Core, nodeLocation common.L
quitCh: make(chan struct{}),
logger: logger,
}
handler.recentBlockReqCache, _ = lru.NewWithExpire(c_recentBlockReqCache, c_recentBlockReqTimeout)
return handler
}

Expand Down Expand Up @@ -91,6 +99,16 @@ func (h *handler) missingBlockLoop() {
for {
select {
case blockRequest := <-h.missingBlockCh:

_, exists := h.recentBlockReqCache.Get(blockRequest.Hash)
if !exists {
// Add the block request to the cache to avoid requesting the same block multiple times
h.recentBlockReqCache.Add(blockRequest.Hash, true)
} else {
// Don't ask for the same block multiple times within a min window
continue
}

go func() {
defer func() {
if r := recover(); r != nil {
Expand Down
Loading