Skip to content

Commit

Permalink
feat(router): request rate limit configuration (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
kon14 authored Oct 23, 2023
1 parent c9b3f80 commit 5629cb3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 12 additions & 0 deletions modules/router/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,16 @@ export default {
default: false,
},
},
rateLimit: {
maxRequests: {
format: 'Number',
default: 50,
doc: 'Maximum number of allowed user requests per reset interval.',
},
resetInterval: {
format: 'Number',
default: 1,
doc: 'Request count reset timeframe. Expressed in seconds.',
},
},
};
7 changes: 4 additions & 3 deletions modules/router/src/security/handlers/rate-limiter/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { Cluster, Redis } from 'ioredis';
import { RateLimiterRedis } from 'rate-limiter-flexible';
import ConduitGrpcSdk, { ConduitError } from '@conduitplatform/grpc-sdk';
import { ConfigController } from '@conduitplatform/module-tools';

export class RateLimiter {
constructor(private readonly grpcSdk: ConduitGrpcSdk) {
const redisClient: Redis | Cluster = this.grpcSdk.redisManager.getClient({
enableOfflineQueue: false,
});
const config = ConfigController.getInstance().config.rateLimit;
this._limiter = new RateLimiterRedis({
storeClient: redisClient,
keyPrefix: 'mainLimiter',
points: 50, // 10 requests
duration: 1, // per 1 second by IP
points: config.maxRequests,
duration: config.resetInterval,
blockDuration: 10,
// inmemoryBlockOnConsumed: 10,
execEvenly: false,
});
}
Expand Down

0 comments on commit 5629cb3

Please sign in to comment.