express-throttle
Advanced tools
Comparing version 1.3.2 to 1.4.0
@@ -13,4 +13,4 @@ "use strict"; | ||
MemoryStore.prototype.set = function(key, value, callback) { | ||
this.cache.set(key, value); | ||
MemoryStore.prototype.set = function(key, bucket, lifetime, callback) { | ||
this.cache.set(key, bucket, lifetime); | ||
callback(); | ||
@@ -17,0 +17,0 @@ }; |
@@ -16,14 +16,6 @@ "use strict"; | ||
if (opts.rate.fixed) { | ||
refill = function(bucket, t) { | ||
bucket.window_start = bucket.window_start || t; | ||
var window1 = Math.floor((bucket.mtime - bucket.window_start) / bucket_settings.period); | ||
var window2 = Math.floor((t - bucket.window_start) / bucket_settings.period); | ||
if (window1 == window2) { | ||
return 0; | ||
} else { | ||
bucket.window_start = t; | ||
return bucket_settings.size; | ||
} | ||
refill = function(bucket, t) { // eslint-disable-line no-unused-vars | ||
// We are not refilling a bucket during the course of its | ||
// lifetime. A new bucket is created when the old one expires. | ||
return 0; | ||
}; | ||
@@ -79,3 +71,3 @@ } else { | ||
store.set(key, bucket, function(err) { | ||
store.set(key, bucket, bucket_settings.period, function(err) { | ||
if (err) { | ||
@@ -99,6 +91,8 @@ return next(err); | ||
"tokens": settings.size, | ||
// creation time | ||
"ctime": ctime, | ||
// last modification time | ||
"mtime": ctime, | ||
// reset time (time left in this period) | ||
"rtime": ctime + settings.period | ||
// expiration time | ||
"etime": ctime + settings.period | ||
}; | ||
@@ -112,3 +106,2 @@ } | ||
bucket.mtime = t; | ||
bucket.rtime = Math.abs(settings.period - t % settings.period); | ||
@@ -115,0 +108,0 @@ if (bucket.tokens >= cost) { |
{ | ||
"name": "express-throttle", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"description": "Request throttling middleware for Express", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tap test/*.js --coverage", | ||
"test": "tap test/*.unit.js --coverage", | ||
"lint": "eslint lib test" | ||
@@ -9,0 +9,0 @@ }, |
# express-throttle | ||
Request throttling middleware for Express framework | ||
[![npm version](https://badge.fury.io/js/express-throttle.svg)](https://badge.fury.io/js/express-throttle) | ||
[![Build Status](https://travis-ci.org/GlurG/express-throttle.svg?branch=master)](https://travis-ci.org/GlurG/express-throttle) | ||
@@ -113,2 +114,4 @@ | ||
res.set("X-Rate-Limit-Remaining", 0); | ||
// bucket.etime = expiration time in Unix epoch ms | ||
res.set("X-Rate-Limit-Reset", bucket.etime); | ||
res.status(503).send("System overloaded, try again at a later time."); | ||
@@ -125,3 +128,3 @@ } | ||
res.set("X-Rate-Limit-Remaining", bucket.tokens); | ||
res.set("X-Rate-Limit-Reset", bucket.rtime); | ||
res.set("X-Rate-Limit-Reset", bucket.etime); | ||
} | ||
@@ -135,3 +138,3 @@ } | ||
} | ||
// These methods must be implemented | ||
@@ -145,3 +148,3 @@ ExternalStorage.prototype.get = function(key, callback) { | ||
ExternalStorage.prototype.set = function(key, bucket, callback) { | ||
ExternalStorage.prototype.set = function(key, bucket, lifetime, callback) { | ||
save(key, bucket, function(err) { | ||
@@ -165,3 +168,3 @@ // err should be null if no errors occurred | ||
`rate`: Determines the number of requests allowed within the specified time unit before subsequent requests get throttled. Must be specified according to the following format: *X/Yt(:fixed)* | ||
`rate`: Determines the number of requests allowed within the specified time before subsequent requests get throttled. Must be specified according to the following format: *X/Yt(:fixed)* | ||
@@ -184,3 +187,4 @@ where *X* and *Y* are integers and *t* is the time unit which can be any of the following: `ms, s, sec, second, m, min, minute, h, hour, d, day` | ||
} | ||
function set(key, bucket, callback) { | ||
function set(key, bucket, lifetime, callback) { | ||
// lifetime (in ms) - same as 'Y' as defined above multiplied by the time unit | ||
// 'bucket' will be an object with the following structure: | ||
@@ -190,4 +194,5 @@ /* | ||
"tokens": Number, (current number of tokens) | ||
"ctime": Number, (creation time) | ||
"mtime": Number, (last modification time) | ||
"rtime": Number (time until next reset) | ||
"etime": Number (expiration time) | ||
} | ||
@@ -223,2 +228,6 @@ */ | ||
}; | ||
``` | ||
``` | ||
## Benchmark | ||
Refer to [this](https://github.com/GlurG/express-throttle/blob/master/Benchmark.md) document for ballparks / guidelines of the performance penalty this middleware will incur. |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
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
31746
15
516
225
2