rate-limiter-flexible
Advanced tools
Comparing version 0.10.0 to 0.10.1
@@ -10,4 +10,4 @@ ## Block Strategy | ||
It can be activated with setup `blockOnPointsConsumed` and `blockDuration` options. | ||
If some actions consume `blockOnPointsConsumed` points, RateLimiterRedis starts using **current process memory** for them | ||
It can be activated with setup `inmemoryBlockOnConsumed` and `inmemoryBlockDuration` options. | ||
If some actions consume `inmemoryBlockOnConsumed` points, RateLimiterRedis starts using **current process memory** for them | ||
All blocked actions with certain key don't request Redis anymore until block expires. | ||
@@ -100,4 +100,4 @@ | ||
duration: 1, | ||
blockOnPointsConsumed: 10, | ||
blockDuration: 30, | ||
inmemoryBlockOnConsumed: 10, | ||
inmemoryBlockDuration: 30, | ||
}, | ||
@@ -104,0 +104,0 @@ ); |
@@ -22,5 +22,5 @@ const RateLimiterStoreAbstract = require('./RateLimiterStoreAbstract'); | ||
if (res.consumedPoints > this.points) { | ||
// Block key for this.blockDuration seconds | ||
if (this.blockOnPointsConsumed > 0 && res.consumedPoints >= this.blockOnPointsConsumed) { | ||
this._blockedKeys.add(rlKey, this.blockDuration); | ||
// Block key for this.inmemoryBlockDuration seconds | ||
if (this.inmemoryBlockOnConsumed > 0 && res.consumedPoints >= this.inmemoryBlockOnConsumed) { | ||
this._blockedKeys.add(rlKey, this.inmemoryBlockDuration); | ||
res.msBeforeNext = this.msBlockDuration; | ||
@@ -27,0 +27,0 @@ } |
@@ -26,5 +26,5 @@ const RateLimiterStoreAbstract = require('./RateLimiterStoreAbstract'); | ||
if (res.consumedPoints > this.points) { | ||
// Block key for this.blockDuration seconds | ||
if (this.blockOnPointsConsumed > 0 && res.consumedPoints >= this.blockOnPointsConsumed) { | ||
this._blockedKeys.add(rlKey, this.blockDuration); | ||
// Block key for this.inmemoryBlockDuration seconds | ||
if (this.inmemoryBlockOnConsumed > 0 && res.consumedPoints >= this.inmemoryBlockOnConsumed) { | ||
this._blockedKeys.add(rlKey, this.inmemoryBlockDuration); | ||
res.msBeforeNext = this.msBlockDuration; | ||
@@ -31,0 +31,0 @@ } |
@@ -10,4 +10,4 @@ const RateLimiterAbstract = require('./RateLimiterAbstract'); | ||
* | ||
* blockOnPointsConsumed: 40, // Number of points when key is blocked | ||
* blockDuration: 10, // Block duration in seconds | ||
* inmemoryBlockOnConsumed: 40, // Number of points when key is blocked | ||
* inmemoryBlockDuration: 10, // Block duration in seconds | ||
* insuranceLimiter: RateLimiterAbstract | ||
@@ -19,4 +19,4 @@ * } | ||
this.blockOnPointsConsumed = opts.blockOnPointsConsumed; | ||
this.blockDuration = opts.blockDuration; | ||
this.inmemoryBlockOnConsumed = opts.inmemoryBlockOnConsumed; | ||
this.inmemoryBlockDuration = opts.inmemoryBlockDuration; | ||
this.insuranceLimiter = opts.insuranceLimiter; | ||
@@ -27,3 +27,3 @@ this._blockedKeys = new BlockedKeys(); | ||
getBlockMsBeforeExpire(rlKey) { | ||
if (this.blockOnPointsConsumed > 0) { | ||
if (this.inmemoryBlockOnConsumed > 0) { | ||
return this._blockedKeys.msBeforeExpire(rlKey); | ||
@@ -49,25 +49,25 @@ } | ||
get blockOnPointsConsumed() { | ||
return this._blockOnPointsConsumed; | ||
get inmemoryBlockOnConsumed() { | ||
return this._inmemoryBlockOnConsumed; | ||
} | ||
set blockOnPointsConsumed(value) { | ||
this._blockOnPointsConsumed = value ? parseInt(value) : 0; | ||
if (this.blockOnPointsConsumed > 0 && this.points >= this.blockOnPointsConsumed) { | ||
throw new Error('blockOnPointsConsumed option must be more than points option'); | ||
set inmemoryBlockOnConsumed(value) { | ||
this._inmemoryBlockOnConsumed = value ? parseInt(value) : 0; | ||
if (this.inmemoryBlockOnConsumed > 0 && this.points > this.inmemoryBlockOnConsumed) { | ||
throw new Error('inmemoryBlockOnConsumed option must be greater or equal "points" option'); | ||
} | ||
} | ||
get blockDuration() { | ||
return this._blockDuration; | ||
get inmemoryBlockDuration() { | ||
return this._inmemoryBlockDuration; | ||
} | ||
get msBlockDuration() { | ||
return this._blockDuration * 1000; | ||
return this._inmemoryBlockDuration * 1000; | ||
} | ||
set blockDuration(value) { | ||
this._blockDuration = value ? parseInt(value) : 0; | ||
if (this.blockDuration > 0 && this.blockOnPointsConsumed === 0) { | ||
throw new Error('blockOnPointsConsumed option must be set up'); | ||
set inmemoryBlockDuration(value) { | ||
this._inmemoryBlockDuration = value ? parseInt(value) : 0; | ||
if (this.inmemoryBlockDuration > 0 && this.inmemoryBlockOnConsumed === 0) { | ||
throw new Error('inmemoryBlockOnConsumed option must be set up'); | ||
} | ||
@@ -74,0 +74,0 @@ } |
{ | ||
"name": "rate-limiter-flexible", | ||
"version": "0.10.0", | ||
"version": "0.10.1", | ||
"description": "Flexible API rate limiter backed by Redis for distributed node.js applications", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -112,7 +112,7 @@ [![Build Status](https://travis-ci.org/animir/node-rate-limiter-flexible.png)](https://travis-ci.org/animir/node-rate-limiter-flexible) | ||
// Redis and Mongo specific | ||
blockOnPointsConsumed: 10, // If 10 points consumed in current duration | ||
blockDuration: 30, // block for 30 seconds in current process memory | ||
// It will be used only on Redis error as insurance | ||
// Can be any implemented limiter like RateLimiterMemory or RateLimiterRedis extended from RateLimiterAbstract | ||
inmemoryBlockOnConsumed: 10, // If 10 points consumed in current duration | ||
inmemoryBlockDuration: 30, // block for 30 seconds in current process memory | ||
insuranceLimiter: new RateLimiterMemory( | ||
// It will be used only on Redis or Mongo error as insurance | ||
// Can be any implemented limiter like RateLimiterMemory or RateLimiterRedis extended from RateLimiterAbstract | ||
{ | ||
@@ -346,8 +346,8 @@ points: 1, // 1 is fair if you have 5 workers and 1 cluster | ||
* `blockOnPointsConsumed` `Default: 0` Against DDoS attacks. Blocked key isn't checked by requesting Redis. | ||
Blocking works in **current process memory**. | ||
Redis is quite fast, however, it may be significantly slowed down on dozens of thousands requests. | ||
* `inmemoryBlockOnConsumed` `Default: 0` Against DDoS attacks. Blocked key isn't checked by requesting Redis or Mongo. | ||
In-memory blocking works in **current process memory**. | ||
Redis and Mongo are quite fast, however, they may be significantly slowed down on dozens of thousands requests. | ||
* `blockDuration` `Default: 0` Block key for `blockDuration` seconds, | ||
if `blockOnPointsConsumed` or more points are consumed | ||
* `inmemoryBlockDuration` `Default: 0` Block key for `inmemoryBlockDuration` seconds, | ||
if `inmemoryBlockOnConsumed` or more points are consumed | ||
@@ -354,0 +354,0 @@ * `insuranceLimiter` `Default: undefined` Instance of RateLimiterAbstract extended object to store limits, |
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
116532