From a16443ee4b5981933318d828b2ede2d0c744b073 Mon Sep 17 00:00:00 2001 From: chuck Date: Thu, 30 May 2019 09:50:14 +0800 Subject: [PATCH 1/4] Fix redis key ttl avoid non-ttl situation --- src/RedisStore.js | 48 ++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/src/RedisStore.js b/src/RedisStore.js index 964a2a7..484d688 100644 --- a/src/RedisStore.js +++ b/src/RedisStore.js @@ -1,21 +1,21 @@ /** * RedisStore - * + * * RedisStore for koa2-ratelimit - * + * * @author Ashok Vishwakarma */ /** * Store - * + * * Existing Store class */ const Store = require('./Store.js'); /** * redis - * + * * promise-redis module * https://github.com/maxbrieiev/promise-redis#readme */ @@ -23,14 +23,14 @@ const redis = require('promise-redis')(); /** * RedisStore - * + * * Class RedisStore */ class RedisStore extends Store { /** * constructor - * @param {*} config - * + * @param {*} config + * * config is redis config */ constructor(config){ @@ -41,14 +41,14 @@ class RedisStore extends Store { /** * _hit * @access private - * @param {*} key - * @param {*} options - * @param {*} weight + * @param {*} key + * @param {*} options + * @param {*} weight */ async _hit(key, options, weight) { let [counter, dateEnd] = await this.client.multi().get(key).ttl(key).exec(); - + if(counter === null) { counter = weight; dateEnd = Date.now() + options.interval; @@ -56,7 +56,13 @@ class RedisStore extends Store { const seconds = Math.ceil(options.interval / 1000); await this.client.setex(key, seconds, counter); }else { - counter = await this.client.incrby(key, weight); + if (dateEnd < 0) { + dateEnd = 1; + } + + [counter] = await this.client.multi().incrby(key, weight).expire(key, dateEnd).exec(); + + dateEnd = Date.now() + dateEnd; } return { @@ -67,11 +73,11 @@ class RedisStore extends Store { /** * incr - * + * * Override incr method from Store class - * @param {*} key - * @param {*} options - * @param {*} weight + * @param {*} key + * @param {*} options + * @param {*} weight */ async incr(key, options, weight) { return await this._hit(key, options, weight); @@ -79,11 +85,11 @@ class RedisStore extends Store { /** * decrement - * + * * Override decrement method from Store class - * @param {*} key - * @param {*} options - * @param {*} weight + * @param {*} key + * @param {*} options + * @param {*} weight */ async decrement(key, options, weight) { await this.client.decrby(key, weight); @@ -91,7 +97,7 @@ class RedisStore extends Store { /** * saveAbuse - * + * * Override saveAbuse method from Store class */ saveAbuse() {} From 6fb764220ddecccac30d77874834d220100eb68b Mon Sep 17 00:00:00 2001 From: chuck Date: Mon, 22 Feb 2021 15:40:51 +0800 Subject: [PATCH 2/4] Use pttl() function for milliseconds --- src/RedisStore.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/RedisStore.js b/src/RedisStore.js index 484d688..a2539f0 100644 --- a/src/RedisStore.js +++ b/src/RedisStore.js @@ -47,7 +47,7 @@ class RedisStore extends Store { */ async _hit(key, options, weight) { - let [counter, dateEnd] = await this.client.multi().get(key).ttl(key).exec(); + let [counter, dateEnd] = await this.client.multi().get(key).pttl(key).exec(); if(counter === null) { counter = weight; @@ -60,7 +60,7 @@ class RedisStore extends Store { dateEnd = 1; } - [counter] = await this.client.multi().incrby(key, weight).expire(key, dateEnd).exec(); + [counter] = await this.client.multi().incrby(key, weight).pexpire(key, dateEnd).exec(); dateEnd = Date.now() + dateEnd; } From 0208700a0707cccb91c9cb8ae5aa8ede72ec0fb9 Mon Sep 17 00:00:00 2001 From: chuck Date: Mon, 22 Feb 2021 15:44:23 +0800 Subject: [PATCH 3/4] Remove to update key expiration with dateEnd --- src/RedisStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RedisStore.js b/src/RedisStore.js index a2539f0..acefca3 100644 --- a/src/RedisStore.js +++ b/src/RedisStore.js @@ -60,7 +60,7 @@ class RedisStore extends Store { dateEnd = 1; } - [counter] = await this.client.multi().incrby(key, weight).pexpire(key, dateEnd).exec(); + counter = await this.client.incrby(key, weight); dateEnd = Date.now() + dateEnd; } From 505d808e530654abf80b4d31f6045269a112f362 Mon Sep 17 00:00:00 2001 From: chuck Date: Mon, 22 Feb 2021 15:45:06 +0800 Subject: [PATCH 4/4] Set to zero if dateEnd is negative --- src/RedisStore.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RedisStore.js b/src/RedisStore.js index acefca3..f5b33be 100644 --- a/src/RedisStore.js +++ b/src/RedisStore.js @@ -57,7 +57,7 @@ class RedisStore extends Store { await this.client.setex(key, seconds, counter); }else { if (dateEnd < 0) { - dateEnd = 1; + dateEnd = 0; } counter = await this.client.incrby(key, weight);