diff --git a/src/common/throttle/redis-throttler.storage.ts b/src/common/throttle/redis-throttler.storage.ts index bfaeec1..d5f3b1a 100644 --- a/src/common/throttle/redis-throttler.storage.ts +++ b/src/common/throttle/redis-throttler.storage.ts @@ -1,19 +1,20 @@ import { Injectable } from '@nestjs/common'; import { ThrottlerStorage } from '@nestjs/throttler'; +import { ThrottlerStorageRecord } from '@nestjs/throttler/dist/throttler-storage-record.interface'; import { RedisService } from '../../infrastructure/redis/redis.service'; @Injectable() export class RedisThrottlerStorage implements ThrottlerStorage { constructor(private readonly redis: RedisService) {} - async increment(key: string, ttl: number): Promise<{ totalHits: number; timeToExpire: number }> { - const redisKey = `throttle:${key}`; + async increment(key: string, ttl: number, limit: number, blockDuration: number, throttlerName: string): Promise { + const redisKey = `throttle:${throttlerName}:${key}`; try { - const result = await this.redis.incr(redisKey); + const hits = await this.redis.incr(redisKey); await this.redis.expire(redisKey, Math.ceil(ttl / 1000)); - return { totalHits: result, timeToExpire: ttl }; + return { totalHits: hits, timeToExpire: ttl, isBlocked: hits > limit, timeToBlockExpire: hits > limit ? blockDuration : 0 }; } catch { - return { totalHits: 1, timeToExpire: ttl }; + return { totalHits: 1, timeToExpire: ttl, isBlocked: false, timeToBlockExpire: 0 }; } } }