From 52926f5282161274b5a36e52e5816ce4fc3dd3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=BDiga=20Kokelj?= Date: Fri, 2 Aug 2024 12:59:33 +0200 Subject: [PATCH] skip adding and deleting requests if rate limiting is disabled --- .../ratelimiter/rate_limiter.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tools/walletextension/ratelimiter/rate_limiter.go b/tools/walletextension/ratelimiter/rate_limiter.go index f2be19ac41..b403725ae3 100644 --- a/tools/walletextension/ratelimiter/rate_limiter.go +++ b/tools/walletextension/ratelimiter/rate_limiter.go @@ -28,6 +28,10 @@ var zeroUUID uuid.UUID // AddRequest adds a new request interval to a user's current requests and returns the UUID. func (rl *RateLimiter) AddRequest(userID common.Address, interval RequestInterval) uuid.UUID { + // If the userComputeTime is 0, do nothing (rate limiting is disabled) + if rl.GetUserComputeTime() == 0 { + return zeroUUID + } rl.mu.Lock() defer rl.mu.Unlock() @@ -45,6 +49,11 @@ func (rl *RateLimiter) AddRequest(userID common.Address, interval RequestInterva // SetRequestEnd updates the end time of a request interval given its UUID. func (rl *RateLimiter) SetRequestEnd(userID common.Address, id uuid.UUID) { + // If the userComputeTime is 0, do nothing (rate limiting is disabled) + if rl.GetUserComputeTime() == 0 { + return + } + if user, userExists := rl.users[userID]; userExists { if request, requestExists := user.CurrentRequests[id]; requestExists { rl.mu.Lock() @@ -205,6 +214,11 @@ func (rl *RateLimiter) PruneRequests() { // periodically prunes the requests that have ended before the rate limiter's window every 10 * window milliseconds func (rl *RateLimiter) periodicPrune() { + // If the userComputeTime is 0, do nothing (rate limiting is disabled) + if rl.GetUserComputeTime() == 0 { + return + } + for { time.Sleep(rl.window * 10) rl.PruneRequests() @@ -212,6 +226,11 @@ func (rl *RateLimiter) periodicPrune() { } func (rl *RateLimiter) logRateLimitedStats() { + // If the userComputeTime is 0, do nothing (rate limiting is disabled) + if rl.GetUserComputeTime() == 0 { + return + } + for { time.Sleep(30 * time.Minute) rl.mu.Lock()