From f870481b7ec5b8204b9e31f2a876e644dfa20e42 Mon Sep 17 00:00:00 2001 From: gop Date: Mon, 11 Oct 2021 14:00:00 -0500 Subject: [PATCH] bugfix: Added rate mutex in ProcRequestRate --- p2p/protocol/handler.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/p2p/protocol/handler.go b/p2p/protocol/handler.go index ca2973895..2e91cd90c 100644 --- a/p2p/protocol/handler.go +++ b/p2p/protocol/handler.go @@ -6,6 +6,7 @@ import ( "io" "math/big" "runtime/debug" + "sync" "time" "github.com/libp2p/go-libp2p/core/network" @@ -35,8 +36,12 @@ type rateTracker struct { var inRateTrackers map[peer.ID]rateTracker var outRateTrackers map[peer.ID]rateTracker +var requestRateMu sync.RWMutex + // Feed the request rate tracker, recomputing the rate, and returning an error if the rate limit is exceeded func ProcRequestRate(peerId peer.ID, inbound bool) error { + requestRateMu.Lock() + defer requestRateMu.Unlock() var rateTrackers *map[peer.ID]rateTracker if inRateTrackers == nil { inRateTrackers = make(map[peer.ID]rateTracker)