rolling-rate-limiter
Advanced tools
Comparing version 0.1.5 to 0.1.6
17
index.js
@@ -35,3 +35,3 @@ var assert = require("assert"); | ||
} | ||
return function (id, cb) { | ||
@@ -42,6 +42,6 @@ if (!cb) { | ||
} | ||
assert.equal(typeof cb, "function", "Callback must be a function."); | ||
var now = microtime.now(); | ||
@@ -58,3 +58,3 @@ var key = namespace + id; | ||
if (err) return cb(err); | ||
var userSet = zrangeToUserSet(resultArr[1]); | ||
@@ -67,3 +67,3 @@ | ||
var remaining = maxInInterval - userSet.length - 1; | ||
if (tooManyInInterval || timeSinceLastRequest < minDifference) { | ||
@@ -90,3 +90,3 @@ result = Math.min(userSet[0] - now + interval, minDifference ? minDifference - timeSinceLastRequest : Infinity); | ||
} | ||
var now = microtime.now(); | ||
@@ -96,2 +96,4 @@ var clearBefore = now - interval; | ||
clearTimeout(timeouts[id]); | ||
delete timeouts[id]; | ||
var userSet = storage[id] = (storage[id] || []).filter(function(timestamp) { | ||
@@ -106,3 +108,3 @@ return timestamp > clearBefore; | ||
var remaining = maxInInterval - userSet.length - 1; | ||
if (tooManyInInterval || timeSinceLastRequest < minDifference) { | ||
@@ -117,2 +119,3 @@ result = Math.min(userSet[0] - now + interval, minDifference ? minDifference - timeSinceLastRequest : Infinity); | ||
delete storage[id]; | ||
delete timeouts[id]; | ||
}, interval / 1000); // convert to miliseconds for javascript timeout | ||
@@ -119,0 +122,0 @@ |
{ | ||
"name": "rolling-rate-limiter", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "Rate limiter that supports a rolling window, either in-memory or backed by redis", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
24224
533