Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Update throttler-storage-redis.service.ts #1734

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 18 additions & 4 deletions src/throttler-storage-redis.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -78,8 +79,17 @@ export class ThrottlerStorageRedisService implements ThrottlerStorageRedis, OnMo
blockDuration: number,
throttlerName: string,
): Promise<ThrottlerStorageRecord> {
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}${keySeparator}${throttlerName}${keySeparator}hits`;
blockKey += `${key}${keySeparator}${throttlerName}${keySeparator}blocked`;

const results: number[] = (await this.redis.call(
'EVAL',
this.scriptSrc,
Expand Down Expand Up @@ -121,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);
Expand Down
Loading