Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pointer issue #839

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update the metric to use bucketLevels[i]?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

// an assumption that the requester name is only provided for authenticated
Expand All @@ -133,6 +133,11 @@ func (d *rateLimiter) checkAllowed(ctx context.Context, params common.RequestPar
}
}

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

return allowed, bucketParams

}
Expand Down
Loading