-
Notifications
You must be signed in to change notification settings - Fork 255
CLDSRV-796: Handle redis unavailability for rate limit #6031
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
Merged
bert-e
merged 5 commits into
development/9.2
from
improvement/CLDSRV-796/handle_redis_unavailability_for_rate_limit
Dec 16, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d2c36f4
CLDSRV-814: prevent premature codecov results
tcarmet eea787a
Merge branch 'improvement/CLDSRV-814-prevent-premature-codecov' into …
bert-e 8121e6d
Merge branch 'w/9.1/improvement/CLDSRV-814-prevent-premature-codecov'…
bert-e c0ee91a
impr(CLDSRV-796): Handle redis unavailability during rate limit token…
tmacro c8fe018
impr(CLDSRV-796): Bump version to 9.2.11
tmacro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| const assert = require('assert'); | ||
| const { RateLimitClient } = require('../../../../../lib/api/apiUtils/rateLimit/client'); | ||
| const { config } = require('../../../../../lib/Config'); | ||
| const { skipIfRateLimitDisabled } = require('./tooling'); | ||
|
|
||
| const testBucket = 'rate-limit-test-bucket'; | ||
|
|
||
| skipIfRateLimitDisabled('RateLimitClient', () => { | ||
| let client; | ||
|
|
||
| before(async () => { | ||
| // Create a test client using the same config as the application | ||
| client = new RateLimitClient(config.localCache); | ||
|
|
||
| // Connect the client (since lazyConnect is true) | ||
| await client.redis.connect(); | ||
| }); | ||
|
|
||
| after(async () => client.redis.quit().catch(() => {})); | ||
|
|
||
| beforeEach(async () => { | ||
| const keys = await client.redis.keys('ratelimit:bucket:*'); | ||
| if (keys.length > 0) { | ||
| await client.redis.del(...keys); | ||
| } | ||
| }); | ||
|
|
||
| describe('isReady', () => { | ||
| it('should return true when client is connected to redis', () => { | ||
| assert.strictEqual(client.isReady(), true); | ||
| }); | ||
|
|
||
| it('should return true when the client is waiting to connect or the first time', () => { | ||
| const client = new RateLimitClient(config.localCache); | ||
| assert.strictEqual(client.isReady(), true); | ||
| }); | ||
| }); | ||
|
|
||
| describe('grantTokens', () => { | ||
| it('should grant requested tokens when quota is available', done => { | ||
| const requested = 5; | ||
| const interval = 100; // 100ms per request = 10 req/s | ||
| const burstCapacity = 1000; // 1000ms burst capacity | ||
|
|
||
| client.grantTokens(testBucket, requested, interval, burstCapacity, (err, granted) => { | ||
| assert.ifError(err); | ||
| assert.strictEqual(granted, requested); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should grant tokens multiple times within burst capacity', done => { | ||
| const requested = 2; | ||
| const interval = 100; // 100ms per request | ||
| const burstCapacity = 1000; // 1000ms burst capacity | ||
|
|
||
| // First request | ||
| client.grantTokens(testBucket, requested, interval, burstCapacity, (err, granted1) => { | ||
| assert.ifError(err); | ||
| assert.strictEqual(granted1, requested); | ||
|
|
||
| // Second request immediately after | ||
| client.grantTokens(testBucket, requested, interval, burstCapacity, (err, granted2) => { | ||
| assert.ifError(err); | ||
| assert.strictEqual(granted2, requested); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should grant partial tokens when request exceeds available capacity', done => { | ||
| const interval = 100; // 100ms per request | ||
| const burstCapacity = 500; // 500ms burst capacity = max 5 tokens | ||
|
|
||
| // Request more tokens than available in burst | ||
| client.grantTokens(testBucket, 10, interval, burstCapacity, (err, granted) => { | ||
| assert.ifError(err); | ||
| // Should grant partial tokens (5 tokens max with 500ms burst) | ||
| assert(granted > 0, 'Should grant at least some tokens'); | ||
| assert(granted <= 5, 'Should not grant more than burst capacity allows'); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should deny tokens (return 0) when quota is exhausted', done => { | ||
| const interval = 100; // 100ms per request | ||
| const burstCapacity = 100; // 100ms burst capacity = max 1 token | ||
|
|
||
| // First request consumes the burst capacity | ||
| client.grantTokens(testBucket, 1, interval, burstCapacity, (err, granted1) => { | ||
| assert.ifError(err); | ||
| assert.strictEqual(granted1, 1); | ||
|
|
||
| // Second request immediately after should be denied | ||
| client.grantTokens(testBucket, 1, interval, burstCapacity, (err, granted2) => { | ||
| assert.ifError(err); | ||
| assert.strictEqual(granted2, 0, 'Should deny tokens when quota exhausted'); | ||
| done(); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should also log the bucket name here
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we also add to default fields, rateLimited: false or something, like we do when we rate limit
like this: https://github.com/scality/cloudserver/blob/development/9.2/lib/api/api.js#L386
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the bucket name. I don't think the
rateLimitedfield makes sense here as we're not making accept/deny decisions.