rate-limiter-flexible
Advanced tools
Comparing version 0.12.3 to 0.12.4
@@ -5,2 +5,5 @@ const RateLimiterStoreAbstract = require('./RateLimiterStoreAbstract'); | ||
const update = function (key, points) { | ||
if (!this._collection) { | ||
return Promise.reject(Error('Mongo connection is not established')); | ||
} | ||
return this._collection.findOneAndUpdate( | ||
@@ -22,2 +25,5 @@ { | ||
const upsertExpire = function (key, points, msDuration) { | ||
if (!this._collection) { | ||
return Promise.reject(Error('Mongo connection is not established')); | ||
} | ||
return this._collection.findOneAndUpdate( | ||
@@ -51,8 +57,12 @@ { | ||
this.mongo = opts.mongo; | ||
if (typeof this.mongo.db === 'function') { | ||
this._collection = this.mongo.db(RateLimiterMongo.getDbName()).collection(this.keyPrefix); | ||
if (typeof this.mongo.then === 'function') { | ||
// If Promise | ||
this.mongo | ||
.then((conn) => { | ||
this.mongo = conn; | ||
this._initCollection(); | ||
}); | ||
} else { | ||
this._collection = this.mongo.db.collection(this.keyPrefix); | ||
this._initCollection(); | ||
} | ||
this._collection.ensureIndex({ expire: -1 }, { expireAfterSeconds: 0 }); | ||
} | ||
@@ -75,2 +85,11 @@ | ||
_initCollection() { | ||
if (typeof this.mongo.db === 'function') { | ||
this._collection = this.mongo.db(RateLimiterMongo.getDbName()).collection(this.keyPrefix); | ||
} else { | ||
this._collection = this.mongo.db.collection(this.keyPrefix); | ||
} | ||
this._collection.ensureIndex({ expire: -1 }, { expireAfterSeconds: 0 }); | ||
} | ||
_getRateLimiterRes(rlKey, changedPoints, result) { | ||
@@ -77,0 +96,0 @@ const res = new RateLimiterRes(); |
{ | ||
"name": "rate-limiter-flexible", | ||
"version": "0.12.3", | ||
"version": "0.12.4", | ||
"description": "Flexible API rate limiter backed by Redis for distributed node.js applications", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -170,16 +170,20 @@ [![Build Status](https://travis-ci.org/animir/node-rate-limiter-flexible.png)](https://travis-ci.org/animir/node-rate-limiter-flexible) | ||
mongoose.createConnection('mongodb://localhost:27017/' + RateLimiterMongo.getDbName(), mongoOpts) | ||
.then((mongo) => { | ||
const opts = { | ||
mongo: mongo, | ||
points: 10, // Number of points | ||
duration: 1, // Per second(s) | ||
}; | ||
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 = { | ||
mongo: mongoConn, | ||
points: 10, // Number of points | ||
duration: 1, // Per second(s) | ||
}; | ||
const rateLimiterMongo = new RateLimiterMongo(opts); | ||
const rateLimiterMongo = new RateLimiterMongo(opts); | ||
// Usage is the same as for RateLimiterRedis | ||
}); | ||
// Or with native mongodb package | ||
/* --- Or with native mongodb package --- */ | ||
const { MongoClient } = require('mongodb'); | ||
@@ -193,21 +197,21 @@ | ||
MongoClient.connect( | ||
const mongoConn = MongoClient.connect( | ||
'mongodb://localhost:27017', | ||
mongoOpts | ||
).then((mongo) => { | ||
const opts = { | ||
mongo: mongo, | ||
points: 10, // Number of points | ||
duration: 1, // Per second(s) | ||
}; | ||
const rateLimiterMongo = new RateLimiterMongo(opts); | ||
// Usage is the same as for RateLimiterRedis | ||
rateLimiterMongo.consume(remoteAddress) | ||
.then(() => {}) | ||
.catch(() => {}); | ||
}); | ||
); | ||
const opts = { | ||
mongo: 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 | ||
@@ -362,10 +366,6 @@ | ||
* `insuranceLimiter` `Default: undefined` Instance of RateLimiterAbstract extended object to store limits, | ||
when Redis comes up with any error. | ||
Additional RateLimiterRedis or RateLimiterMemory can be used as insurance. | ||
Be careful when use RateLimiterMemory in cluster or in distributed app. | ||
It may result to floating number of allowed actions. | ||
If an action with a same `key` is launched on one worker several times in sequence, | ||
limiter will reach out of points soon. | ||
Omit it if you want strictly deal with store errors | ||
when Redis or Mongo comes up with any error. | ||
All data from `insuranceLimiter` is NOT copied to parent limiter, when error gone | ||
**Note:** `insuranceLimiter` automatically setup `blockDuration` and `execEvenly` | ||
@@ -372,0 +372,0 @@ to same values as in parent to avoid unexpected behaviour |
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
120318
1034