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(router): add info log for consumed limits #959

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Changes from all commits
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
9 changes: 7 additions & 2 deletions modules/router/src/security/handlers/rate-limiter/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Cluster, Redis } from 'ioredis';
import { RateLimiterRedis } from 'rate-limiter-flexible';
import { RateLimiterRedis, RateLimiterRes } from 'rate-limiter-flexible';
import ConduitGrpcSdk, { ConduitError } from '@conduitplatform/grpc-sdk';
import { ConfigController } from '@conduitplatform/module-tools';

Expand Down Expand Up @@ -35,13 +35,18 @@ export class RateLimiter {
req.headers['cf-connecting-ip'] ||
req.headers['x-original-forwarded-for'] ||
req.headers['x-forwarded-for'] ||
req.headers['x-real-ip'] ||
req.ip;
self._limiter
.consume(ip)
.then(() => {
next();
})
.catch(() => {
.catch((rateLimiterRes: RateLimiterRes) => {
ConduitGrpcSdk.Logger.info(
`RATE_LIMIT: ${ip} exceeded rate limit, ${rateLimiterRes.consumedPoints} consumed.
${rateLimiterRes.msBeforeNext} before next request is allowed.`,
);
next(new ConduitError('RATE_LIMIT', 429, 'Too Many Requests'));
});
};
Expand Down
Loading