From 5c11267c6c5d2750b755acd58b7c9e8198e5f139 Mon Sep 17 00:00:00 2001 From: codeHui Date: Thu, 6 Jan 2022 12:06:24 +0800 Subject: [PATCH] fix 2 bug: 1,"dateEnd===-1" may happen just after ttl expires then "await this.client.incrBy(key, weight)" will set ttl to -1; we count this low probability requests as the previous period and check "dateEnd===-1" for the next request 2,add "dateEnd = Date.now() + dateEnd * 1000;" the unit of Date.now() is millisecond --- src/RedisStore.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/RedisStore.js b/src/RedisStore.js index a72e6aa..fa3e554 100644 --- a/src/RedisStore.js +++ b/src/RedisStore.js @@ -51,15 +51,18 @@ class RedisStore extends Store { async _hit(key, options, weight) { let [counter, dateEnd] = await this.client.multi().get(key).ttl(key).exec(); - - if(counter === null) { + + //check "dateEnd===-1", the reason is below + if(counter === null || dateEnd===-1) { counter = weight; dateEnd = Date.now() + options.interval; const seconds = Math.ceil(options.interval / 1000); await this.client.setEx(key, seconds.toString(), counter.toString()); }else { + //"dateEnd===-1" may happen just after ttl expires then "await this.client.incrBy(key, weight)" will set ttl to -1; we count this low probability requests as the previous period and check "dateEnd===-1" for the next request counter = await this.client.incrBy(key, weight); + dateEnd = Date.now() + dateEnd * 1000; } return {