rate-limiter-flexible
Advanced tools
Comparing version 0.15.0 to 0.15.1
@@ -54,69 +54,32 @@ const RateLimiterStoreAbstract = require('./RateLimiterStoreAbstract'); | ||
_block(rlKey, initPoints, msDuration) { | ||
_upsert(rlKey, points, msDuration, forceExpire = false) { | ||
return new Promise((resolve, reject) => { | ||
this.redis.set(rlKey, initPoints, 'EX', Math.floor(msDuration / 1000), (err) => { | ||
if (err) { | ||
return this._handleError(err, 'block', resolve, reject, this.parseKey(rlKey), msDuration / 1000); | ||
} | ||
const secDuration = Math.floor(msDuration / 1000); | ||
if (forceExpire) { | ||
this.redis.multi() | ||
.set(rlKey, points, 'EX', secDuration) | ||
.pttl(rlKey) | ||
.exec((err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
resolve(new RateLimiterRes(0, msDuration, initPoints)); | ||
}); | ||
}); | ||
} | ||
return resolve(['FORCE', points, res[1]]); | ||
}); | ||
} else { | ||
this.redis.multi() | ||
.set(rlKey, 0, 'EX', secDuration, 'NX') | ||
.incrby(rlKey, points) | ||
.pttl(rlKey) | ||
.exec((err, res) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
/** | ||
* | ||
* @param key | ||
* @param pointsToConsume | ||
* @returns {Promise<any>} | ||
*/ | ||
consume(key, pointsToConsume = 1) { | ||
return new Promise((resolve, reject) => { | ||
const rlKey = this.getKey(key); | ||
const inmemoryBlockMsBeforeExpire = this.getInmemoryBlockMsBeforeExpire(rlKey); | ||
if (inmemoryBlockMsBeforeExpire > 0) { | ||
return reject(new RateLimiterRes(0, inmemoryBlockMsBeforeExpire)); | ||
return resolve(res); | ||
}); | ||
} | ||
this.redis.multi() | ||
.set(rlKey, 0, 'EX', this.duration, 'NX') | ||
.incrby(rlKey, pointsToConsume) | ||
.pttl(rlKey) | ||
.exec((err, results) => { | ||
if (err) { | ||
this._handleError(err, 'consume', resolve, reject, key, pointsToConsume); | ||
} else { | ||
this._afterConsume(resolve, reject, rlKey, pointsToConsume, results); | ||
} | ||
}); | ||
}); | ||
} | ||
penalty(key, points = 1) { | ||
const rlKey = this.getKey(key); | ||
return new Promise((resolve, reject) => { | ||
this.redis.incrby(rlKey, points, (err, consumedPoints) => { | ||
if (err) { | ||
this._handleError(err, 'penalty', resolve, reject, key, points); | ||
} else { | ||
resolve(new RateLimiterRes(this.points - consumedPoints, 0, consumedPoints)); | ||
} | ||
}); | ||
}); | ||
} | ||
reward(key, points = 1) { | ||
const rlKey = this.getKey(key); | ||
return new Promise((resolve, reject) => { | ||
this.redis.incrby(rlKey, -points, (err, consumedPoints) => { | ||
if (err) { | ||
this._handleError(err, 'reward', resolve, reject, key, points); | ||
} else { | ||
resolve(new RateLimiterRes(this.points - consumedPoints, 0, consumedPoints)); | ||
} | ||
}); | ||
}); | ||
} | ||
_get(rlKey) { | ||
@@ -123,0 +86,0 @@ return new Promise((resolve, reject) => { |
@@ -203,3 +203,3 @@ const RateLimiterAbstract = require('./RateLimiterAbstract'); | ||
.catch((err) => { | ||
this._handleError(err, 'reward', resolve, reject, key, -points); | ||
this._handleError(err, 'reward', resolve, reject, key, points); | ||
}); | ||
@@ -206,0 +206,0 @@ }); |
{ | ||
"name": "rate-limiter-flexible", | ||
"version": "0.15.0", | ||
"version": "0.15.1", | ||
"description": "Flexible API rate limiter backed by Redis for distributed node.js applications", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
89486
26
1404