rate-limiter-flexible
Advanced tools
Comparing version 0.8.1 to 0.8.2
@@ -20,3 +20,9 @@ const RateLimiterAbstract = require('./RateLimiterAbstract'); | ||
const afterConsume = function (resolve, reject, rlKey, results) { | ||
const [resSet, consumed, resTtlMs] = results; | ||
let [resSet, consumed, resTtlMs] = results; | ||
// Support ioredis results format | ||
if (Array.isArray(resSet)) { | ||
resSet = resSet[1]; | ||
consumed = consumed[1]; | ||
resTtlMs = resTtlMs[1]; | ||
} | ||
const res = new RateLimiterRes(); | ||
@@ -23,0 +29,0 @@ let isFirstInDuration = resSet === 'OK'; |
{ | ||
"name": "rate-limiter-flexible", | ||
"version": "0.8.1", | ||
"version": "0.8.2", | ||
"description": "Flexible API rate limiter backed by Redis for distributed node.js applications", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "./node_modules/istanbul/lib/cli.js cover ./node_modules/.bin/_mocha lib/**/**.test.js", | ||
"test": "./node_modules/istanbul/lib/cli.js cover ./node_modules/.bin/_mocha --recursive", | ||
"debug-test": "mocha --inspect-brk lib/**/**.test.js", | ||
@@ -9,0 +9,0 @@ "coveralls": "cat ./coverage/lcov.info | node node_modules/.bin/coveralls", |
@@ -20,3 +20,3 @@ [![Build Status](https://travis-ci.org/animir/node-rate-limiter-flexible.png)](https://travis-ci.org/animir/node-rate-limiter-flexible) | ||
* backed on native Promises | ||
* works in Cluster without additional software | ||
* works in Cluster without additional software [See RateLimiterCluster benchmark and detailed description here](https://github.com/animir/node-rate-limiter-flexible/blob/master/CLUSTER.md) | ||
* actions can be done evenly over duration window to cut off picks | ||
@@ -61,10 +61,19 @@ * no race conditions | ||
Redis client must be created with offline queue switched off | ||
It supports both `redis` and `ioredis` clients. | ||
Redis client must be created with offline queue switched off. | ||
```javascript | ||
const redis = require('redis'); | ||
const redisClient = redis.createClient({ enable_offline_queue: false }); | ||
const Redis = require('ioredis'); | ||
const redisClient = new Redis({ | ||
options: { | ||
enableOfflineQueue: false | ||
} | ||
}); | ||
const { RateLimiterRedis, RateLimiterMemory } = require('rate-limiter-flexible'); | ||
const redisClient = redis.createClient({ enable_offline_queue: false }); | ||
// It is recommended to process Redis errors and setup some reconnection strategy | ||
@@ -98,3 +107,3 @@ redisClient.on('error', (err) => { | ||
rateLimiterRedis.consume(remoteAddress) | ||
.then(() => { | ||
.then((rateLimiterRes) => { | ||
// ... Some app logic here ... | ||
@@ -104,6 +113,6 @@ | ||
rateLimiterRedis.penalty(remoteAddress, 3) | ||
.then((consumedPoints) => {}); | ||
.then((rateLimiterRes) => {}); | ||
// or rise number of points for current duration | ||
rateLimiterRedis.reward(remoteAddress, 2) | ||
.then((consumedPoints) => {}); | ||
.then((rateLimiterRes) => {}); | ||
}) | ||
@@ -251,3 +260,3 @@ .catch((rejRes) => { | ||
**rejected** when some Redis error happened, where reject reason `rejRes` is Error object | ||
* only for RateLimiterCluster: **rejected** when `timeotMs` exceeded, where reject reason `rejRes` is Error object | ||
* only for RateLimiterCluster: **rejected** when `timeoutMs` exceeded, where reject reason `rejRes` is Error object | ||
@@ -264,3 +273,3 @@ ### rateLimiter.reward(key, points = 1) | ||
**rejected** when some Redis error happened, where reject reason `rejRes` is Error object | ||
* only for RateLimiterCluster: **rejected** when `timeotMs` exceeded, where reject reason `rejRes` is Error object | ||
* only for RateLimiterCluster: **rejected** when `timeoutMs` exceeded, where reject reason `rejRes` is Error object | ||
@@ -267,0 +276,0 @@ ## Contribution |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
277
92153
24
686