From 0eb8baefafa15778093215202ccd24135a0fd43a Mon Sep 17 00:00:00 2001 From: Ravi Bisht <47188858+ravibisht@users.noreply.github.com> Date: Sun, 18 Aug 2024 22:29:16 +0530 Subject: [PATCH 1/3] Update throttler-storage-redis.service.ts Add `key separator` for throttler. existing `{` one make harder to clear ratelimiter keys in redis through pattern search. And it will introduced feature to setup user defined key separator. --- src/throttler-storage-redis.service.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/throttler-storage-redis.service.ts b/src/throttler-storage-redis.service.ts index 8750d567..1aad6ef3 100644 --- a/src/throttler-storage-redis.service.ts +++ b/src/throttler-storage-redis.service.ts @@ -78,8 +78,17 @@ export class ThrottlerStorageRedisService implements ThrottlerStorageRedis, OnMo blockDuration: number, throttlerName: string, ): Promise { - const hitKey = `${this.redis.options.keyPrefix}{${key}:${throttlerName}}:hits`; - const blockKey = `${this.redis.options.keyPrefix}{${key}:${throttlerName}}:blocked`; + + let hitKey = this.redis.options.keyPrefix; + let blockKey = this.redis.options.keyPrefix; + + if (hitKey !== undefined && hitKey !== null && hitKey != ''){ + hitKey += this.keySeparator; + blockKey += this.keySeparator; + } + hitKey += `${key}:${throttlerName}:hits`; + blockKey += `${key}:${throttlerName}:blocked`; + const results: number[] = (await this.redis.call( 'EVAL', this.scriptSrc, From 8697a6bb16212371814f9754e20718ce68094adf Mon Sep 17 00:00:00 2001 From: Ravi Bisht <47188858+ravibisht@users.noreply.github.com> Date: Sun, 18 Aug 2024 22:31:43 +0530 Subject: [PATCH 2/3] Update throttler-storage-redis.service.ts --- src/throttler-storage-redis.service.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/throttler-storage-redis.service.ts b/src/throttler-storage-redis.service.ts index 1aad6ef3..3b246cef 100644 --- a/src/throttler-storage-redis.service.ts +++ b/src/throttler-storage-redis.service.ts @@ -8,7 +8,8 @@ export class ThrottlerStorageRedisService implements ThrottlerStorageRedis, OnMo scriptSrc: string; redis: Redis | Cluster; disconnectRequired?: boolean; - + private keySeparator: string = '-'; + constructor(redis?: Redis); constructor(cluster?: Cluster); constructor(options?: RedisOptions); @@ -130,7 +131,11 @@ export class ThrottlerStorageRedisService implements ThrottlerStorageRedis, OnMo timeToBlockExpire: Math.ceil(timeToBlockExpire / 1000), }; } - + + setKeySeparator(separator: string) { + this.keySeparator = separator; + } + onModuleDestroy() { if (this.disconnectRequired) { this.redis?.disconnect(false); From 624c2e8e8f0ffe458d093381f3e8a737da76387f Mon Sep 17 00:00:00 2001 From: Ravi Bisht <47188858+ravibisht@users.noreply.github.com> Date: Sun, 18 Aug 2024 22:49:30 +0530 Subject: [PATCH 3/3] Update throttler-storage-redis.service.ts --- src/throttler-storage-redis.service.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/throttler-storage-redis.service.ts b/src/throttler-storage-redis.service.ts index 3b246cef..4c8cddcc 100644 --- a/src/throttler-storage-redis.service.ts +++ b/src/throttler-storage-redis.service.ts @@ -8,7 +8,7 @@ export class ThrottlerStorageRedisService implements ThrottlerStorageRedis, OnMo scriptSrc: string; redis: Redis | Cluster; disconnectRequired?: boolean; - private keySeparator: string = '-'; + private keySeparator: string = ':'; constructor(redis?: Redis); constructor(cluster?: Cluster); @@ -87,8 +87,8 @@ export class ThrottlerStorageRedisService implements ThrottlerStorageRedis, OnMo hitKey += this.keySeparator; blockKey += this.keySeparator; } - hitKey += `${key}:${throttlerName}:hits`; - blockKey += `${key}:${throttlerName}:blocked`; + hitKey += `${key}${keySeparator}${throttlerName}${keySeparator}hits`; + blockKey += `${key}${keySeparator}${throttlerName}${keySeparator}blocked`; const results: number[] = (await this.redis.call( 'EVAL',