rate-limiter-flexible
Advanced tools
Comparing version 0.16.0 to 0.16.1
{ | ||
"name": "rate-limiter-flexible", | ||
"version": "0.16.0", | ||
"version": "0.16.1", | ||
"description": "Flexible API rate limiter backed by Redis for distributed node.js applications", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -119,3 +119,3 @@ ## RateLimiterPostgres | ||
new RateLimiterPostgres({ | ||
storeClient: mysql, | ||
storeClient: pgClient, | ||
points: 5, // Number of points | ||
@@ -138,2 +138,2 @@ duration: 1, // Per second(s) | ||
1xx - 0, 2xx - 8985, 3xx - 0, 4xx - 21024, 5xx - 0 | ||
``` | ||
``` |
126
README.md
@@ -9,3 +9,3 @@ [![Build Status](https://travis-ci.org/animir/node-rate-limiter-flexible.png)](https://travis-ci.org/animir/node-rate-limiter-flexible) | ||
<img src="rlflx-logo-small.png" width="50" alt="Logo"/> | ||
<img src="img/rlflx-logo-small.png" width="50" alt="Logo"/> | ||
@@ -33,9 +33,28 @@ ## node-rate-limiter-flexible | ||
### Example | ||
```javascript | ||
const opts = { | ||
points: 6, // 6 points | ||
duration: 1, // Per second | ||
}; | ||
const rateLimiter = new RateLimiterMemory(opts); | ||
rateLimiter.consume(remoteAddress, 2) // consume 2 points | ||
.then((rateLimiterRes) => { | ||
// 2 points consumed | ||
}) | ||
.catch((rateLimiterRes) => { | ||
// Not enough points to consume | ||
}); | ||
``` | ||
### Links | ||
* [RateLimiterRedis](#ratelimiterredis) | ||
* [RateLimiterMongo](#ratelimitermongo) | ||
* [RateLimiterMySQL](https://github.com/animir/node-rate-limiter-flexible/blob/master/MYSQL.md) | ||
* [RateLimiterPostgreSQL](https://github.com/animir/node-rate-limiter-flexible/blob/master/POSTGRES.md) | ||
* [RateLimiterCluster](#ratelimitercluster) | ||
* [RateLimiterMongo](https://github.com/animir/node-rate-limiter-flexible/wiki/Mongo) | ||
* [RateLimiterMySQL](https://github.com/animir/node-rate-limiter-flexible/blob/master/MYSQL.md) (support Sequelize and Knex) | ||
* [RateLimiterPostgreSQL](https://github.com/animir/node-rate-limiter-flexible/blob/master/POSTGRES.md) (support Sequelize and Knex) | ||
* [RateLimiterCluster](https://github.com/animir/node-rate-limiter-flexible/wiki/Cluster) | ||
* [RateLimiterMemory](#ratelimitermemory) | ||
@@ -296,97 +315,2 @@ * [RateLimiterUnion](#ratelimiterunion) Combine 2 or more limiters to act as single | ||
### RateLimiterMongo | ||
MongoDB >=3.2 | ||
It supports `mongodb` native and `mongoose` packages | ||
[See RateLimiterMongo benchmark here](https://github.com/animir/node-rate-limiter-flexible/blob/master/MONGO.md) | ||
```javascript | ||
const { RateLimiterMongo } = require('rate-limiter-flexible'); | ||
const mongoose = require('mongoose'); | ||
const mongoOpts = { | ||
reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect | ||
reconnectInterval: 100, // Reconnect every 100ms | ||
}; | ||
mongoose.connect('mongodb://127.0.0.1:27017/' + RateLimiterMongo.getDbName()) | ||
.catch((err) => {}); | ||
const mongoConn = mongoose.connection; | ||
// Or | ||
const mongoConn = mongoose.createConnection('mongodb://127.0.0.1:27017/' + RateLimiterMongo.getDbName(), mongoOpts); | ||
const opts = { | ||
storeClient: mongoConn, | ||
points: 10, // Number of points | ||
duration: 1, // Per second(s) | ||
}; | ||
const rateLimiterMongo = new RateLimiterMongo(opts); | ||
// Usage is the same as for RateLimiterRedis | ||
/* --- Or with native mongodb package --- */ | ||
const { MongoClient } = require('mongodb'); | ||
const mongoOpts = { | ||
useNewUrlParser: true, | ||
reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect | ||
reconnectInterval: 100, // Reconnect every 100ms | ||
}; | ||
const mongoConn = MongoClient.connect( | ||
'mongodb://localhost:27017', | ||
mongoOpts | ||
); | ||
const opts = { | ||
storeClient: mongoConn, | ||
points: 10, // Number of points | ||
duration: 1, // Per second(s) | ||
}; | ||
const rateLimiterMongo = new RateLimiterMongo(opts); | ||
// Usage is the same as for RateLimiterRedis | ||
``` | ||
Connection to Mongo takes milliseconds, so any method of rate limiter is rejected with Error, until connection established | ||
`insuranceLimiter` can be setup to avoid errors, but all changes won't be written from `insuranceLimiter` to `RateLimiterMongo` when connection established | ||
### RateLimiterCluster | ||
Note: it doesn't work with PM2 yet | ||
RateLimiterCluster performs limiting using IPC. | ||
Each request is sent to master process, which handles all the limits, then master send results back to worker. | ||
[See RateLimiterCluster benchmark and detailed description here](https://github.com/animir/node-rate-limiter-flexible/blob/master/CLUSTER.md) | ||
```javascript | ||
const cluster = require('cluster'); | ||
const numCPUs = require('os').cpus().length; | ||
const { RateLimiterClusterMaster, RateLimiterCluster } = require('rate-limiter-flexible'); | ||
if (cluster.isMaster) { | ||
// Doesn't require any options, it is only storage and messages handler | ||
new RateLimiterClusterMaster(); | ||
for (let i = 0; i < numCPUs; i++) { | ||
cluster.fork(); | ||
} | ||
} else { | ||
const rateLimiter = new RateLimiterCluster({ | ||
keyPrefix: 'myclusterlimiter', // Must be unique for each limiter | ||
points: 100, | ||
duration: 1, | ||
timeoutMs: 3000 // Promise is rejected, if master doesn't answer for 3 secs | ||
}); | ||
// Usage is the same as for RateLimiterRedis | ||
} | ||
``` | ||
### RateLimiterMemory | ||
@@ -494,2 +418,2 @@ | ||
All other methods depends on store. See `RateLimiterPostgres` for example. | ||
All other methods depends on store. See `RateLimiterRedis` or `RateLimiterPostgres` for example. |
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
83854
24
416