-
Notifications
You must be signed in to change notification settings - Fork 93
feat: rework IPRateLimiter with Redis integration #4662
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
Open
quiet-node
wants to merge
5
commits into
main
Choose a base branch
from
4599-ratelimiter-refactor-storage-selection-to-use-factory-pattern
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
03109f3
feat: rework IPRateLimiter with Redis integration
quiet-node a59d8a1
Update RedisRateLimitStore.spec.ts
quiet-node 16fa735
fix: fixed acceptance tests
quiet-node b115487
fix: removed unused logger in IPRateLimiterService
quiet-node 59f6388
fix: cleaned up RateLimitStoreFactory test
quiet-node 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
38 changes: 38 additions & 0 deletions
38
packages/relay/src/lib/services/rateLimiterService/RateLimitStoreFactory.ts
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,38 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { Logger } from 'pino'; | ||
| import { Counter } from 'prom-client'; | ||
| import { RedisClientType } from 'redis'; | ||
|
|
||
| import { RateLimitStore } from '../../types'; | ||
| import { LruRateLimitStore } from './LruRateLimitStore'; | ||
| import { RedisRateLimitStore } from './RedisRateLimitStore'; | ||
|
|
||
| /** | ||
| * Factory for creating RateLimitStore instances. | ||
| * | ||
| * Encapsulates the logic for selecting the appropriate storage implementation | ||
| * based on available infrastructure (Redis vs in-memory). | ||
| */ | ||
| export class RateLimitStoreFactory { | ||
| /** | ||
| * Creates a RateLimitStore instance. | ||
| * | ||
| * @param logger - Logger instance for the store. | ||
| * @param duration - Time window in milliseconds for rate limiting. | ||
| * @param rateLimitStoreFailureCounter - Optional counter for tracking store failures. | ||
| * @param redisClient - Optional Redis client. If provided, creates Redis-backed storage; | ||
| * otherwise creates local in-memory storage. | ||
| * @returns A RateLimitStore implementation. | ||
| */ | ||
| static create( | ||
| logger: Logger, | ||
| duration: number, | ||
| rateLimitStoreFailureCounter?: Counter, | ||
| redisClient?: RedisClientType, | ||
| ): RateLimitStore { | ||
| return redisClient | ||
| ? new RedisRateLimitStore(redisClient, logger, duration, rateLimitStoreFailureCounter) | ||
| : new LruRateLimitStore(duration); | ||
| } | ||
| } |
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
65 changes: 65 additions & 0 deletions
65
packages/relay/tests/lib/services/rateLimiterService/RateLimitStoreFactory.spec.ts
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,65 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import chai, { expect } from 'chai'; | ||
| import chaiAsPromised from 'chai-as-promised'; | ||
| import { Logger, pino } from 'pino'; | ||
| import { Counter, Registry } from 'prom-client'; | ||
|
|
||
| import { LruRateLimitStore } from '../../../../src/lib/services/rateLimiterService/LruRateLimitStore'; | ||
| import { RateLimitStoreFactory } from '../../../../src/lib/services/rateLimiterService/RateLimitStoreFactory'; | ||
| import { RedisRateLimitStore } from '../../../../src/lib/services/rateLimiterService/RedisRateLimitStore'; | ||
|
|
||
| chai.use(chaiAsPromised); | ||
|
|
||
| describe('RateLimitStoreFactory', () => { | ||
| let logger: Logger; | ||
| let registry: Registry; | ||
| let rateLimitStoreFailureCounter: Counter; | ||
| const testDuration = 5000; | ||
| const mockRedisClient = { eval: () => {} } as any; | ||
|
|
||
| beforeEach(() => { | ||
| logger = pino({ level: 'silent' }); | ||
| registry = new Registry(); | ||
| rateLimitStoreFailureCounter = new Counter({ | ||
| name: 'test_rate_limit_store_failure', | ||
| help: 'Test counter for rate limit store failures', | ||
| labelNames: ['store_type', 'method'], | ||
| registers: [registry], | ||
| }); | ||
| }); | ||
|
|
||
| describe('create', () => { | ||
| it('should return LruRateLimitStore when redisClient is undefined', () => { | ||
| const store = RateLimitStoreFactory.create(logger, testDuration, rateLimitStoreFailureCounter, undefined); | ||
| expect(store).to.be.instanceOf(LruRateLimitStore); | ||
| }); | ||
|
|
||
| it('should return RedisRateLimitStore when redisClient is provided', () => { | ||
| const store = RateLimitStoreFactory.create(logger, testDuration, rateLimitStoreFailureCounter, mockRedisClient); | ||
|
|
||
| expect(store).to.be.instanceOf(RedisRateLimitStore); | ||
| }); | ||
|
|
||
| it('should return LruRateLimitStore without failure counter', () => { | ||
| const store = RateLimitStoreFactory.create(logger, testDuration); | ||
|
|
||
| expect(store).to.be.instanceOf(LruRateLimitStore); | ||
| }); | ||
|
|
||
| it('should return RedisRateLimitStore with failure counter', () => { | ||
| const store = RateLimitStoreFactory.create(logger, testDuration, rateLimitStoreFailureCounter, mockRedisClient); | ||
|
|
||
| expect(store).to.be.instanceOf(RedisRateLimitStore); | ||
| }); | ||
|
|
||
| it('should create different store instances on multiple calls', () => { | ||
| const store1 = RateLimitStoreFactory.create(logger, testDuration); | ||
| const store2 = RateLimitStoreFactory.create(logger, testDuration, rateLimitStoreFailureCounter, mockRedisClient); | ||
|
|
||
| expect(store1).to.not.equal(store2); | ||
| expect(store1).to.be.instanceOf(LruRateLimitStore); | ||
| expect(store2).to.be.instanceOf(RedisRateLimitStore); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
Amaziing, much cleaner!
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.
Great glad you find it better!