Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
})
})