Skip to content

Commit

Permalink
feat: add block duration
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-yarmosh committed Sep 27, 2024
1 parent 33b078e commit 2b1dfc9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/lib/rate-limiter/rate-limiter-get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ export const anonymousRateLimiter = new RateLimiterRedis({
keyPrefix: 'rate:get:anon',
points: config.get<number>('measurement.rateLimit.get.anonymousLimit'),
duration: config.get<number>('measurement.rateLimit.get.reset'),
blockDuration: 5,
});

export const authenticatedRateLimiter = new RateLimiterRedis({
storeClient: redisClient,
keyPrefix: 'rate:get:auth',
points: config.get<number>('measurement.rateLimit.get.authenticatedLimit'),
duration: config.get<number>('measurement.rateLimit.get.reset'),
blockDuration: 5,
});

const getRateLimiter = (ctx: ExtendedContext, extraId?: string): {
Expand Down
5 changes: 2 additions & 3 deletions test/tests/integration/ratelimit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('rate limiter', () => {
await anonymousGetRateLimiter.set(`${clientIpv6}:${id}`, 1000, 0);
const response = await requestAgent.get(`/v1/measurements/${id}`).send().expect(429) as Response;

expect(response.headers['retry-after']).to.equal('2');
expect(response.headers['retry-after']).to.equal('5');
});
});

Expand Down Expand Up @@ -274,13 +274,12 @@ describe('rate limiter', () => {
type: 'ping',
target: 'jsdelivr.com',
}).expect(202) as Response;
console.log(id);
await authenticatedGetRateLimiter.set(`89da69bd-a236-4ab7-9c5d-b5f52ce09959:${id}`, 1000, 0);
const response = await requestAgent.get(`/v1/measurements/${id}`)
.set('Authorization', 'Bearer qz5kdukfcr3vggv3xbujvjwvirkpkkpx')
.send().expect(429) as Response;

expect(response.headers['retry-after']).to.equal('2');
expect(response.headers['retry-after']).to.equal('5');
});
});

Expand Down

0 comments on commit 2b1dfc9

Please sign in to comment.