rate-limiter-flexible
Advanced tools
Comparing version 0.17.3 to 0.18.0
@@ -9,2 +9,3 @@ module.exports = class RateLimiterAbstract { | ||
* execEvenly: false, // Execute allowed actions evenly over duration | ||
* execEvenlyMinDelayMs: duration * 1000 / points, // ms, works with execEvenly=true option | ||
* keyPrefix: 'rlflx', | ||
@@ -18,2 +19,3 @@ * } | ||
this.execEvenly = opts.execEvenly; | ||
this.execEvenlyMinDelayMs = opts.execEvenlyMinDelayMs; | ||
this.keyPrefix = opts.keyPrefix; | ||
@@ -62,2 +64,10 @@ } | ||
get execEvenlyMinDelayMs() { | ||
return this._execEvenlyMinDelayMs; | ||
} | ||
set execEvenlyMinDelayMs(value) { | ||
this._execEvenlyMinDelayMs = typeof value === 'undefined' ? Math.ceil(this.msDuration / this.points) : value; | ||
} | ||
get keyPrefix() { | ||
@@ -64,0 +74,0 @@ return this._keyPrefix; |
@@ -32,3 +32,6 @@ const RateLimiterAbstract = require('./RateLimiterAbstract'); | ||
// Execute evenly | ||
const delay = Math.ceil(res.msBeforeNext / ((this.points - res.consumedPoints) + 2)); | ||
let delay = Math.ceil(res.msBeforeNext / (res.remainingPoints + 2)); | ||
if (delay < this.execEvenlyMinDelayMs) { | ||
delay = res.consumedPoints * this.execEvenlyMinDelayMs; | ||
} | ||
@@ -35,0 +38,0 @@ setTimeout(resolve, delay, res); |
@@ -103,10 +103,9 @@ const RateLimiterStoreAbstract = require('./RateLimiterStoreAbstract'); | ||
res = null; | ||
} else if (Array.isArray(points)) { | ||
// Support ioredis format | ||
res.unshift([null, 'GET']); | ||
} else { | ||
if (Array.isArray(points)) { | ||
// Support ioredis format | ||
res.unshift([null, 'GET']); | ||
} else { | ||
res.unshift('GET'); | ||
} | ||
res.unshift('GET'); | ||
} | ||
resolve(res); | ||
@@ -113,0 +112,0 @@ } |
@@ -73,3 +73,6 @@ const RateLimiterAbstract = require('./RateLimiterAbstract'); | ||
} else if (this.execEvenly && res.msBeforeNext > 0 && !res.isFirstInDuration) { | ||
const delay = Math.ceil(res.msBeforeNext / (res.remainingPoints + 2)); | ||
let delay = Math.ceil(res.msBeforeNext / (res.remainingPoints + 2)); | ||
if (delay < this.execEvenlyMinDelayMs) { | ||
delay = res.consumedPoints * this.execEvenlyMinDelayMs; | ||
} | ||
@@ -76,0 +79,0 @@ setTimeout(resolve, delay, res); |
{ | ||
"name": "rate-limiter-flexible", | ||
"version": "0.17.3", | ||
"version": "0.18.0", | ||
"description": "Flexible API rate limiter backed by Redis for distributed node.js applications", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -36,3 +36,3 @@ [![Build Status](https://travis-ci.org/animir/node-rate-limiter-flexible.png)](https://travis-ci.org/animir/node-rate-limiter-flexible) | ||
* works in Cluster without additional software [See RateLimiterCluster benchmark and detailed description here](https://github.com/animir/node-rate-limiter-flexible/wiki/Cluster) | ||
* actions can be done evenly over duration window to cut off picks | ||
* shape traffic with Leaky Bucket analogy [Read about Leaky Bucket analogy](https://github.com/animir/node-rate-limiter-flexible/wiki/Leaky-Bucket-Analogy---execute-actions-evenly) | ||
* no race conditions | ||
@@ -115,6 +115,11 @@ * covered by tests | ||
First action in duration is executed without delay. | ||
All next allowed actions in current duration are delayed by formula `msBeforeDurationEnd / (remainingPoints + 2)` | ||
It allows to cut off load peaks. | ||
Note: it isn't recommended to use it for long duration, as it may delay action for too long | ||
All next allowed actions in current duration are delayed by formula `msBeforeDurationEnd / (remainingPoints + 2)` | ||
with minimum delay of `duration * 1000 / points` | ||
It allows to cut off load peaks similar way to Leaky Bucket. [Read detailed description here.](https://github.com/animir/node-rate-limiter-flexible/wiki/Leaky-Bucket-Analogy---execute-actions-evenly) | ||
Note: it isn't recommended to use it for long duration and few points, | ||
as it may delay action for too long with default `execEvenlyMinDelayMs`. | ||
* `execEvenlyMinDelayMs` `Default: duration * 1000 / points` Sets minimum delay in milliseconds, when action is delayed with `execEvenly` | ||
* `blockDuration` `Default: 0` If positive number and consumed more than points in current duration, | ||
@@ -121,0 +126,0 @@ block for `blockDuration` seconds. |
70991
1837
377