From 6156f5585897516e4c683b649a64b5f6323bcdad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDiga=20Kokelj?= Date: Mon, 19 Aug 2024 12:08:59 +0200 Subject: [PATCH] fix concurrency issue in SumComputeTime --- tools/walletextension/ratelimiter/rate_limiter.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/walletextension/ratelimiter/rate_limiter.go b/tools/walletextension/ratelimiter/rate_limiter.go index f264108e5b..38efc4a047 100644 --- a/tools/walletextension/ratelimiter/rate_limiter.go +++ b/tools/walletextension/ratelimiter/rate_limiter.go @@ -104,6 +104,9 @@ func (rl *RateLimiter) SumComputeTime(userID common.Address) time.Duration { var totalComputeTime time.Duration if user, exists := rl.users[userID]; exists { + user.mu.RLock() // lock the user to prevent changes while reading + defer user.mu.RUnlock() + cutoff := time.Now().Add(-rl.window) for _, interval := range user.CurrentRequests { // if the request has ended and it's within the window, add the compute time