mirror of
https://github.com/status-im/go-waku.git
synced 2025-02-09 04:04:10 +00:00
fix: fatal error: concurrent map writes (#1265)
This commit is contained in:
parent
1608cf2b0b
commit
d2bdab893b
@ -6,6 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p/core/host"
|
"github.com/libp2p/go-libp2p/core/host"
|
||||||
"github.com/libp2p/go-libp2p/core/peer"
|
"github.com/libp2p/go-libp2p/core/peer"
|
||||||
@ -73,6 +74,7 @@ type WakuStore struct {
|
|||||||
|
|
||||||
defaultRatelimit rate.Limit
|
defaultRatelimit rate.Limit
|
||||||
rateLimiters map[peer.ID]*rate.Limiter
|
rateLimiters map[peer.ID]*rate.Limiter
|
||||||
|
rateLimitersMux sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewWakuStore is used to instantiate a StoreV3 client
|
// NewWakuStore is used to instantiate a StoreV3 client
|
||||||
@ -297,11 +299,13 @@ func (s *WakuStore) queryFrom(ctx context.Context, storeRequest *pb.StoreQueryRe
|
|||||||
logger.Debug("sending store request")
|
logger.Debug("sending store request")
|
||||||
|
|
||||||
if !params.skipRatelimit {
|
if !params.skipRatelimit {
|
||||||
|
s.rateLimitersMux.Lock()
|
||||||
rateLimiter, ok := s.rateLimiters[params.selectedPeer]
|
rateLimiter, ok := s.rateLimiters[params.selectedPeer]
|
||||||
if !ok {
|
if !ok {
|
||||||
rateLimiter = rate.NewLimiter(s.defaultRatelimit, 1)
|
rateLimiter = rate.NewLimiter(s.defaultRatelimit, 1)
|
||||||
s.rateLimiters[params.selectedPeer] = rateLimiter
|
s.rateLimiters[params.selectedPeer] = rateLimiter
|
||||||
}
|
}
|
||||||
|
s.rateLimitersMux.Unlock()
|
||||||
err := rateLimiter.Wait(ctx)
|
err := rateLimiter.Wait(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user