From fdc153bd57913972bf51be760a28a390382af221 Mon Sep 17 00:00:00 2001 From: JaeMyeongSon Date: Wed, 23 Apr 2025 16:54:33 +0900 Subject: [PATCH] Fix: Correct hit counter increment error when blocked --- .../src/throttler-storage-redis.service.ts | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/packages/throttler-storage-redis/src/throttler-storage-redis.service.ts b/packages/throttler-storage-redis/src/throttler-storage-redis.service.ts index 8750d56..b7b43c5 100644 --- a/packages/throttler-storage-redis/src/throttler-storage-redis.service.ts +++ b/packages/throttler-storage-redis/src/throttler-storage-redis.service.ts @@ -38,6 +38,17 @@ export class ThrottlerStorageRedisService implements ThrottlerStorageRedis, OnMo local limit = tonumber(ARGV[3]) local blockDuration = tonumber(ARGV[4]) + local isBlocked = redis.call('GET', blockKey) + if isBlocked then + local timeToBlockExpire = redis.call('PTTL', blockKey) + if timeToBlockExpire > 0 then + local currentHits = tonumber(redis.call('GET', hitKey)) or 0 + return { currentHits, redis.call('PTTL', hitKey), 1, timeToBlockExpire } + else + redis.call('DEL', blockKey) + end + end + local totalHits = redis.call('INCR', hitKey) local timeToExpire = redis.call('PTTL', hitKey) @@ -46,26 +57,12 @@ export class ThrottlerStorageRedisService implements ThrottlerStorageRedis, OnMo timeToExpire = ttl end - local isBlocked = redis.call('GET', blockKey) - local timeToBlockExpire = 0 - - if isBlocked then - timeToBlockExpire = redis.call('PTTL', blockKey) - elseif totalHits > limit then + if totalHits > limit then redis.call('SET', blockKey, 1, 'PX', blockDuration) - isBlocked = '1' - timeToBlockExpire = blockDuration - end - - if isBlocked and timeToBlockExpire <= 0 then - redis.call('DEL', blockKey) - redis.call('SET', hitKey, 1, 'PX', ttl) - totalHits = 1 - timeToExpire = ttl - isBlocked = false + return { totalHits, timeToExpire, 1, blockDuration } end - return { totalHits, timeToExpire, isBlocked and 1 or 0, timeToBlockExpire } + return { totalHits, timeToExpire, 0, 0 } ` .replace(/^\s+/gm, '') .trim();