Skip to content

Commit

Permalink
refactor: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Mar 27, 2019
1 parent c305fd7 commit a774e90
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -107,8 +107,8 @@ Given an `id`, returns a Promise with the status of the limit with the following

##### id

*Required*</br>
Type: `string`
Default: `this.id`

The identifier to limit against (typically a user id).

Expand Down
19 changes: 10 additions & 9 deletions src/index.js
Expand Up @@ -24,20 +24,21 @@ module.exports = class Limiter {
assert(max, 'max required')
assert(duration, 'duration required')

const { db } = this
const key = `${this.namespace}:${id}`
const now = microtime.now()
const start = now - duration * 1000

const pipeline = db.multi()
pipeline.zremrangebyscore([key, 0, start])
pipeline.zcard([key])
if (decrease) pipeline.zadd([key, now, now])
pipeline.zrange([key, 0, 0])
pipeline.zrange([key, -max, -max])
pipeline.pexpire([key, duration])
const res = await pipeline.exec()
const operations = [
['zremrangebyscore', key, 0, start],
['zcard', key],
['zrange', key, 0, 0],
['zrange', key, -max, -max],
['pexpire', key, duration]
]

if (decrease) operations.splice(2, 0, ['zadd', key, now, now])

const res = await this.db.multi(operations).exec()
const count = parseInt(res[1][1])
const oldest = parseInt(res[decrease ? 3 : 2][1])
const oldestInRange = parseInt(res[decrease ? 4 : 3][1])
Expand Down
42 changes: 21 additions & 21 deletions test/index.js
Expand Up @@ -19,10 +19,10 @@ const RateLimiter = require('..')

describe('.total', function () {
it('should represent the total limit per reset period', async function () {
let limit = new RateLimiter({
const limit = new RateLimiter({
max: 5,
id: 'something',
db: db
db
})
const res = await limit.get()
should(res.total).equal(5)
Expand All @@ -31,11 +31,11 @@ const RateLimiter = require('..')

describe('.remaining', function () {
it('should represent the number of requests remaining in the reset period', async function () {
let limit = new RateLimiter({
const limit = new RateLimiter({
max: 5,
duration: 100000,
id: 'something',
db: db
db
})

let res
Expand All @@ -50,11 +50,11 @@ const RateLimiter = require('..')

describe('.reset', function () {
it('should represent the next reset time in UTC epoch seconds', async function () {
let limit = new RateLimiter({
const limit = new RateLimiter({
max: 5,
duration: 60000,
id: 'something',
db: db
db
})
const res = await limit.get()
let left = res.reset - Date.now() / 1000
Expand All @@ -66,10 +66,10 @@ const RateLimiter = require('..')

describe('when the limit is exceeded', function () {
it('should retain .remaining at 0', async function () {
let limit = new RateLimiter({
const limit = new RateLimiter({
max: 2,
id: 'something',
db: db
db
})

let res
Expand All @@ -82,10 +82,10 @@ const RateLimiter = require('..')
})

it('should return an increasing reset time after each call', async function () {
let limit = new RateLimiter({
const limit = new RateLimiter({
max: 2,
id: 'something',
db: db
db
})

await limit.get()
Expand All @@ -101,11 +101,11 @@ const RateLimiter = require('..')
describe('when the duration is exceeded', function () {
it('should reset', async function () {
this.timeout(5000)
let limit = new RateLimiter({
const limit = new RateLimiter({
duration: 2000,
max: 2,
id: 'something',
db: db
db
})

let res
Expand All @@ -124,11 +124,11 @@ const RateLimiter = require('..')

describe('when multiple successive calls are made', function () {
it('the next calls should not create again the limiter in Redis', async function () {
let limit = new RateLimiter({
const limit = new RateLimiter({
duration: 10000,
max: 2,
id: 'something',
db: db
db
})

let res
Expand All @@ -138,11 +138,11 @@ const RateLimiter = require('..')
should(res.remaining).equal(1)
})
it('updating the count should keep all TTLs in sync', async function () {
let limit = new RateLimiter({
const limit = new RateLimiter({
duration: 10000,
max: 2,
id: 'something',
db: db
db
})
await limit.get() // All good here.
await limit.get()
Expand All @@ -164,11 +164,11 @@ const RateLimiter = require('..')

describe('when trying to decrease before setting value', function () {
it('should create with ttl when trying to decrease', async function () {
let limit = new RateLimiter({
const limit = new RateLimiter({
duration: 10000,
max: 2,
id: 'something',
db: db
db
})

db.setex('limit:something:count', -1, 1, async function () {
Expand All @@ -187,7 +187,7 @@ const RateLimiter = require('..')
let clientsCount = 7
let max = 5
let left = max
let limits = []
const limits = []

for (let i = 0; i < clientsCount; ++i) {
limits.push(
Expand Down Expand Up @@ -360,11 +360,11 @@ const RateLimiter = require('..')

describe('when get is called with decrease == false', function () {
it('should not decrement the remaining value', async function () {
let limit = new RateLimiter({
const limit = new RateLimiter({
max: 5,
duration: 100000,
id: 'something',
db: db
db
})

let res
Expand Down

0 comments on commit a774e90

Please sign in to comment.