Skip to content

Commit

Permalink
skip adding and deleting requests if rate limiting is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
zkokelj committed Aug 2, 2024
1 parent 8acb9ac commit 52926f5
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions tools/walletextension/ratelimiter/rate_limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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()
Expand Down Expand Up @@ -205,13 +214,23 @@ 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()
}
}

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()
Expand Down

0 comments on commit 52926f5

Please sign in to comment.