From 31beaedc0f65e09bb217317e37116a1b04d5e7c8 Mon Sep 17 00:00:00 2001 From: amanda Date: Fri, 20 Jul 2018 11:54:59 +0800 Subject: [PATCH] old data test and delete the old ones by score --- src/index.js | 2 +- test/index.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index c81d742..10f85dc 100644 --- a/src/index.js +++ b/src/index.js @@ -26,7 +26,7 @@ module.exports = class Limiter { const res = await db .multi() - .zrange([key, 0, start, 'WITHSCORES']) + .zremrangebyscore([key, 0, start]) .zcard([key]) .zadd([key, now, now]) .zrange([key, 0, 0]) diff --git a/test/index.js b/test/index.js index bd23b55..9cd8fcb 100644 --- a/test/index.js +++ b/test/index.js @@ -319,5 +319,27 @@ const RateLimiter = require('..') .and.be.above(10) }) }) + + describe('when having old data earlier then the duration ', function () { + it('the old request data eariler then the duration time should be ignore', async function () { + this.timeout(20000) + const limit = new RateLimiter({ + db, + max: 5, + id: 'something', + duration: 5000 + }) + let res = await limit.get() + should(res.remaining).equal(5) + + let times = 6 + do { + res = await limit.get() + ;(res.remaining > 0).should.be.true() + await delay(2000) + times-- + } while (times > 0) + }) + }) }) })