Skip to content

Commit

Permalink
Fix pointer issue (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
mooselumph authored Oct 28, 2024
1 parent 5fe3e91 commit 9bd44f4
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions common/ratelimit/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ func (d *rateLimiter) checkAllowed(ctx context.Context, params common.RequestPar
}
}

bucketLevels := make([]time.Duration, len(d.globalRateParams.BucketSizes))

// Check whether the request is allowed based on the rate

// Get interval since last request
interval := time.Since(bucketParams.LastRequestTime)
bucketParams.LastRequestTime = time.Now().UTC()
lastRequestTime := time.Now().UTC()

// Calculate updated bucket levels
allowed := true
Expand All @@ -113,13 +115,11 @@ func (d *rateLimiter) checkAllowed(ctx context.Context, params common.RequestPar
// Determine bucket deduction
deduction := time.Microsecond * time.Duration(1e6*float32(params.BlobSize)/float32(params.Rate)/d.globalRateParams.Multipliers[i])

prevLevel := bucketParams.BucketLevels[i]

// Update the bucket level
bucketParams.BucketLevels[i] = getBucketLevel(bucketParams.BucketLevels[i], size, interval, deduction)
allowed = allowed && bucketParams.BucketLevels[i] > 0
bucketLevels[i] = getBucketLevel(bucketParams.BucketLevels[i], size, interval, deduction)
allowed = allowed && bucketLevels[i] > 0

d.logger.Debug("Bucket level updated", "key", params.RequesterID, "name", params.RequesterName, "prevLevel", prevLevel, "level", bucketParams.BucketLevels[i], "size", size, "interval", interval, "deduction", deduction, "allowed", allowed)
d.logger.Debug("Bucket level updated", "key", params.RequesterID, "name", params.RequesterName, "prevLevel", bucketParams.BucketLevels[i], "level", bucketLevels[i], "size", size, "interval", interval, "deduction", deduction, "allowed", allowed)

// Update metrics only if the requester name is provided. We're making
// an assumption that the requester name is only provided for authenticated
Expand All @@ -129,10 +129,15 @@ func (d *rateLimiter) checkAllowed(ctx context.Context, params common.RequestPar
"requester_id": params.RequesterID,
"requester_name": params.RequesterName,
"bucket_index": strconv.Itoa(i),
}).Set(float64(bucketParams.BucketLevels[i]))
}).Set(float64(bucketLevels[i]))
}
}

bucketParams = &common.RateBucketParams{
LastRequestTime: lastRequestTime,
BucketLevels: bucketLevels,
}

return allowed, bucketParams

}
Expand Down

0 comments on commit 9bd44f4

Please sign in to comment.