Comparing version 1.0.5 to 1.1.0
@@ -18,6 +18,6 @@ var TokenBucket = require('./tokenBucket'); | ||
interval, null); | ||
// Fill the token bucket to start | ||
this.tokenBucket.content = tokensPerInterval; | ||
this.curIntervalStart = +new Date(); | ||
@@ -33,3 +33,3 @@ this.tokensThisInterval = 0; | ||
fireImmediately: false, | ||
/** | ||
@@ -48,11 +48,11 @@ * Remove the requested number of tokens and fire the given callback. If the | ||
if (count > this.tokenBucket.bucketSize) { | ||
callback('Requested tokens ' + count + | ||
process.nextTick(callback.bind(null, 'Requested tokens ' + count + | ||
' exceeds maximum tokens per interval ' + this.tokenBucket.bucketSize, | ||
null); | ||
null)); | ||
return false; | ||
} | ||
var self = this; | ||
var now = Date.now(); | ||
// Advance the current interval and reset the current interval token count | ||
@@ -64,3 +64,3 @@ // if needed | ||
} | ||
// If we don't have enough tokens left in this interval, wait until the | ||
@@ -70,3 +70,3 @@ // next interval | ||
if (this.fireImmediately) { | ||
callback(null, -1); | ||
process.nextTick(callback.bind(null, null, -1)); | ||
} else { | ||
@@ -82,3 +82,3 @@ var waitInterval = Math.ceil( | ||
} | ||
// Remove the requested number of tokens from the token bucket | ||
@@ -85,0 +85,0 @@ return this.tokenBucket.removeTokens(count, afterTokensRemoved); |
@@ -6,3 +6,3 @@ | ||
* @author John Hurliman <jhurliman@cull.tv> | ||
* | ||
* | ||
* @param {Number} bucketSize Maximum number of tokens to hold in the bucket. | ||
@@ -20,3 +20,3 @@ * Also known as the burst rate. | ||
this.tokensPerInterval = tokensPerInterval; | ||
if (typeof interval === 'string') { | ||
@@ -36,3 +36,3 @@ switch (interval) { | ||
} | ||
this.parentBucket = parentBucket; | ||
@@ -50,3 +50,3 @@ this.content = 0; | ||
lastDrip: 0, | ||
/** | ||
@@ -64,23 +64,23 @@ * Remove the requested number of tokens and fire the given callback. If the | ||
var self = this; | ||
// Is this an infinite size bucket? | ||
if (!this.bucketSize) { | ||
callback(null, count, Number.POSITIVE_INFINITY); | ||
process.nextTick(callback.bind(null, null, count, Number.POSITIVE_INFINITY)); | ||
return true; | ||
} | ||
// Make sure the bucket can hold the requested number of tokens | ||
if (count > this.bucketSize) { | ||
callback('Requested tokens ' + count + ' exceeds bucket size ' + | ||
this.bucketSize, null); | ||
process.nextTick(callback.bind(null, 'Requested tokens ' + count + ' exceeds bucket size ' + | ||
this.bucketSize, null)); | ||
return false; | ||
} | ||
// Drip new tokens into this bucket | ||
this.drip(); | ||
// If we don't have enough tokens in this bucket, come back later | ||
if (count > this.content) | ||
return comeBackLater(); | ||
if (this.parentBucket) { | ||
@@ -90,7 +90,7 @@ // Remove the requested from the parent bucket first | ||
if (err) return callback(err, null); | ||
// Check that we still have enough tokens in this bucket | ||
if (count > self.content) | ||
return comeBackLater(); | ||
// Tokens were removed from the parent bucket, now remove them from | ||
@@ -106,6 +106,6 @@ // this bucket and fire the callback. Note that we look at the current | ||
this.content -= count; | ||
callback(null, this.content); | ||
process.nextTick(callback.bind(null, null, this.content)); | ||
return true; | ||
} | ||
function comeBackLater() { | ||
@@ -132,7 +132,7 @@ // How long do we need to wait to make up the difference in tokens? | ||
return true; | ||
// Make sure the bucket can hold the requested number of tokens | ||
if (count > this.bucketSize) | ||
return false; | ||
// Drip new tokens into this bucket | ||
@@ -163,7 +163,7 @@ this.drip(); | ||
} | ||
var now = +new Date(); | ||
var deltaMS = Math.max(now - this.lastDrip, 0); | ||
this.lastDrip = now; | ||
var dripAmount = deltaMS * (this.tokensPerInterval / this.interval); | ||
@@ -170,0 +170,0 @@ this.content = Math.min(this.content + dripAmount, this.bucketSize); |
{ | ||
"name": "limiter", | ||
"description": "A generic rate limiter for node.js. Useful for API clients, web crawling, or other tasks that need to be throttled", | ||
"version": "1.0.5", | ||
"author": "John Hurliman <jhurliman@cull.tv>", | ||
"dependencies": { }, | ||
"version": "1.1.0", | ||
"author": "John Hurliman <jhurliman@jhurliman.org>", | ||
"scripts": { | ||
"test": "vows --spec" | ||
}, | ||
"dependencies": { | ||
}, | ||
"devDependencies": { | ||
"assert": "0.4.9", | ||
"vows": "0.6.3" | ||
"assert": "1.3.0", | ||
"vows": "0.8.1" | ||
}, | ||
@@ -11,0 +15,0 @@ "keywords": ["rate", "limiting", "throttling"], |
@@ -1,4 +0,6 @@ | ||
# limiter # | ||
[![Build Status](https://travis-ci.org/jhurliman/node-rate-limiter.png)](https://travis-ci.org/jhurliman/node-rate-limiter) | ||
[![NPM Downloads](https://img.shields.io/npm/dm/rate-limiter.svg?style=flat)](https://www.npmjs.com/package/limiter) | ||
Provides a generic rate limiter for node.js. Useful for API clients, web | ||
@@ -5,0 +7,0 @@ crawling, or other tasks that need to be throttled. Two classes are exposed, |
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
20198
10
343
141