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

feat: token bucket rate limiting #91

Merged
merged 7 commits into from
Nov 5, 2024
Merged

feat: token bucket rate limiting #91

merged 7 commits into from
Nov 5, 2024

Conversation

craigzour
Copy link
Contributor

@craigzour craigzour commented Oct 24, 2024

Summary | Résumé

Should not be merged until V1 is released to production (unless we decide to include it in V1).

Context: cds-snc/platform-forms-client#3947

  • Implements token bucket rate limiter using two different bucket capacities
    • Low rate limit is 500 requests per minute
    • High rate limit is 1000 requests per minute
  • Renames and moves cache (in lib/utils) to redisClientAdapter (in lib/integration/redis). The rate limiter code uses Redis to get a value that will be set by the web application. Since there is no cache concept involved here I decided to rename the two cache related functions. This way we can use it everywhere we need to access Redis and it also simplifies the mocks in our unit tests.

Test instructions

  • Modify the config.ts file to lower the token bucket capacity for both lowRateLimiterConfiguration and highRateLimiterConfiguration
  • Send request to the API and observe both the response headers (there should be some new X-RateLimit properties) and response HTTP code (will be 429 when you have reached the limit)
  • If you want to enable the high capacity bucket token just add a new entry in Redis with the following key/value pair: rate-limit:${formId} => "high"

@craigzour craigzour self-assigned this Oct 24, 2024
@craigzour craigzour force-pushed the feature/rate-limiter branch 6 times, most recently from 243ac32 to 7cf683d Compare October 31, 2024 16:25
// Rate limiting

export const lowRateLimiterConfiguration: TokenBucketConfiguration = {
capacity: 500,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A maximum of 500 requests per minute is going to be what we ask our prototyping partners to follow for V1. When the Rate limiter is released we will actually be able to see whether it is a good starting point for our users.

};

export const highRateLimiterConfiguration: TokenBucketConfiguration = {
capacity: 1000,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The high capacity token bucket is not going to be used for now as the web app feature to enable it is not a priority at the moment.

@craigzour craigzour marked this pull request as ready for review October 31, 2024 16:34
@craigzour craigzour force-pushed the feature/rate-limiter branch from 7cf683d to ea4861f Compare October 31, 2024 16:35
@craigzour craigzour marked this pull request as draft October 31, 2024 19:12
@craigzour craigzour force-pushed the feature/rate-limiter branch 2 times, most recently from 132262b to f400655 Compare November 1, 2024 17:09
@craigzour craigzour marked this pull request as ready for review November 1, 2024 17:12
@craigzour craigzour force-pushed the feature/rate-limiter branch from f400655 to 3340dfa Compare November 4, 2024 15:47
src/middleware/globalErrorHandler.ts Show resolved Hide resolved
test/lib/rateLimiting/tokenBucketLimiter.test.ts Outdated Show resolved Hide resolved
test/lib/rateLimiting/tokenBucketLimiter.test.ts Outdated Show resolved Hide resolved
@craigzour craigzour merged commit 201b6c4 into main Nov 5, 2024
4 checks passed
@craigzour craigzour deleted the feature/rate-limiter branch November 5, 2024 14:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants