bottleneck
Advanced tools
Comparing version 1.0.0 to 1.1.0
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.7.1 | ||
(function() { | ||
@@ -11,27 +11,29 @@ var Bottleneck, | ||
this.minTime = minTime != null ? minTime : 0; | ||
this.nextRequest = Date.now(); | ||
this.nbRunning = 0; | ||
this.queue = []; | ||
this.timeouts = []; | ||
this._nextRequest = Date.now(); | ||
this._nbRunning = 0; | ||
this._queue = []; | ||
this._timeouts = []; | ||
} | ||
Bottleneck.prototype._tryToRun = function() { | ||
var done, next, wait, | ||
_this = this; | ||
if ((this.nbRunning < this.maxNb || this.maxNb <= 0) && this.queue.length > 0) { | ||
this.nbRunning++; | ||
wait = Math.max(this.nextRequest - Date.now(), 0); | ||
this.nextRequest = Date.now() + wait + this.minTime; | ||
next = this.queue.shift(); | ||
var done, next, wait; | ||
if ((this._nbRunning < this.maxNb || this.maxNb <= 0) && this._queue.length > 0) { | ||
this._nbRunning++; | ||
wait = Math.max(this._nextRequest - Date.now(), 0); | ||
this._nextRequest = Date.now() + wait + this.minTime; | ||
next = this._queue.shift(); | ||
done = false; | ||
return this.timeouts.push(setTimeout(function() { | ||
return next.task.apply({}, next.args.concat(function() { | ||
if (!done) { | ||
done = true; | ||
_this.nbRunning--; | ||
_this._tryToRun(); | ||
return next.cb.apply({}, Array.prototype.slice.call(arguments, 0)); | ||
} | ||
})); | ||
}, wait)); | ||
return this._timeouts.push(setTimeout((function(_this) { | ||
return function() { | ||
return next.task.apply({}, next.args.concat(function() { | ||
var _ref; | ||
if (!done) { | ||
done = true; | ||
_this._nbRunning--; | ||
_this._tryToRun(); | ||
return (_ref = next.cb) != null ? _ref.apply({}, Array.prototype.slice.call(arguments, 0)) : void 0; | ||
} | ||
})); | ||
}; | ||
})(this), wait)); | ||
} | ||
@@ -43,3 +45,3 @@ }; | ||
task = arguments[0], args = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), cb = arguments[_i++]; | ||
this.queue.push({ | ||
this._queue.push({ | ||
task: task, | ||
@@ -52,5 +54,11 @@ args: args, | ||
Bottleneck.prototype.changeSettings = function(maxNb, minTime) { | ||
this.maxNb = maxNb != null ? maxNb : this.maxNb; | ||
this.minTime = minTime != null ? minTime : this.minTime; | ||
return this; | ||
}; | ||
Bottleneck.prototype.stopAll = function() { | ||
var a, _i, _len, _ref; | ||
_ref = this.timeouts; | ||
_ref = this._timeouts; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
@@ -72,3 +80,4 @@ a = _ref[_i]; | ||
},{}],2:[function(require,module,exports){ | ||
var global=typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {};// Generated by CoffeeScript 1.6.3 | ||
(function (global){ | ||
// Generated by CoffeeScript 1.7.1 | ||
(function() { | ||
@@ -83,2 +92,3 @@ module.exports = require("./Bottleneck"); | ||
}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | ||
},{"./Bottleneck":1}]},{},[2]) |
@@ -1,1 +0,1 @@ | ||
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){(function(){var Bottleneck,__slice=[].slice;Bottleneck=function(){function Bottleneck(maxNb,minTime){this.maxNb=maxNb!=null?maxNb:0;this.minTime=minTime!=null?minTime:0;this.nextRequest=Date.now();this.nbRunning=0;this.queue=[];this.timeouts=[]}Bottleneck.prototype._tryToRun=function(){var done,next,wait,_this=this;if((this.nbRunning<this.maxNb||this.maxNb<=0)&&this.queue.length>0){this.nbRunning++;wait=Math.max(this.nextRequest-Date.now(),0);this.nextRequest=Date.now()+wait+this.minTime;next=this.queue.shift();done=false;return this.timeouts.push(setTimeout(function(){return next.task.apply({},next.args.concat(function(){if(!done){done=true;_this.nbRunning--;_this._tryToRun();return next.cb.apply({},Array.prototype.slice.call(arguments,0))}}))},wait))}};Bottleneck.prototype.submit=function(){var args,cb,task,_i;task=arguments[0],args=3<=arguments.length?__slice.call(arguments,1,_i=arguments.length-1):(_i=1,[]),cb=arguments[_i++];this.queue.push({task:task,args:args,cb:cb});return this._tryToRun()};Bottleneck.prototype.stopAll=function(){var a,_i,_len,_ref;_ref=this.timeouts;for(_i=0,_len=_ref.length;_i<_len;_i++){a=_ref[_i];clearTimeout(a)}return this._tryToRun=function(){}};return Bottleneck}();module.exports=Bottleneck}).call(this)},{}],2:[function(require,module,exports){var global=typeof self!=="undefined"?self:typeof window!=="undefined"?window:{};(function(){module.exports=require("./Bottleneck");if(global.window!=null){global.window.Bottleneck=module.exports}}).call(this)},{"./Bottleneck":1}]},{},[2]); | ||
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){(function(){var Bottleneck,__slice=[].slice;Bottleneck=function(){function Bottleneck(maxNb,minTime){this.maxNb=maxNb!=null?maxNb:0;this.minTime=minTime!=null?minTime:0;this._nextRequest=Date.now();this._nbRunning=0;this._queue=[];this._timeouts=[]}Bottleneck.prototype._tryToRun=function(){var done,next,wait;if((this._nbRunning<this.maxNb||this.maxNb<=0)&&this._queue.length>0){this._nbRunning++;wait=Math.max(this._nextRequest-Date.now(),0);this._nextRequest=Date.now()+wait+this.minTime;next=this._queue.shift();done=false;return this._timeouts.push(setTimeout(function(_this){return function(){return next.task.apply({},next.args.concat(function(){var _ref;if(!done){done=true;_this._nbRunning--;_this._tryToRun();return(_ref=next.cb)!=null?_ref.apply({},Array.prototype.slice.call(arguments,0)):void 0}}))}}(this),wait))}};Bottleneck.prototype.submit=function(){var args,cb,task,_i;task=arguments[0],args=3<=arguments.length?__slice.call(arguments,1,_i=arguments.length-1):(_i=1,[]),cb=arguments[_i++];this._queue.push({task:task,args:args,cb:cb});return this._tryToRun()};Bottleneck.prototype.changeSettings=function(maxNb,minTime){this.maxNb=maxNb!=null?maxNb:this.maxNb;this.minTime=minTime!=null?minTime:this.minTime;return this};Bottleneck.prototype.stopAll=function(){var a,_i,_len,_ref;_ref=this._timeouts;for(_i=0,_len=_ref.length;_i<_len;_i++){a=_ref[_i];clearTimeout(a)}return this._tryToRun=function(){}};return Bottleneck}();module.exports=Bottleneck}).call(this)},{}],2:[function(require,module,exports){(function(global){(function(){module.exports=require("./Bottleneck");if(global.window!=null){global.window.Bottleneck=module.exports}}).call(this)}).call(this,typeof self!=="undefined"?self:typeof window!=="undefined"?window:{})},{"./Bottleneck":1}]},{},[2]); |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.7.1 | ||
(function() { | ||
@@ -10,27 +10,29 @@ var Bottleneck, | ||
this.minTime = minTime != null ? minTime : 0; | ||
this.nextRequest = Date.now(); | ||
this.nbRunning = 0; | ||
this.queue = []; | ||
this.timeouts = []; | ||
this._nextRequest = Date.now(); | ||
this._nbRunning = 0; | ||
this._queue = []; | ||
this._timeouts = []; | ||
} | ||
Bottleneck.prototype._tryToRun = function() { | ||
var done, next, wait, | ||
_this = this; | ||
if ((this.nbRunning < this.maxNb || this.maxNb <= 0) && this.queue.length > 0) { | ||
this.nbRunning++; | ||
wait = Math.max(this.nextRequest - Date.now(), 0); | ||
this.nextRequest = Date.now() + wait + this.minTime; | ||
next = this.queue.shift(); | ||
var done, next, wait; | ||
if ((this._nbRunning < this.maxNb || this.maxNb <= 0) && this._queue.length > 0) { | ||
this._nbRunning++; | ||
wait = Math.max(this._nextRequest - Date.now(), 0); | ||
this._nextRequest = Date.now() + wait + this.minTime; | ||
next = this._queue.shift(); | ||
done = false; | ||
return this.timeouts.push(setTimeout(function() { | ||
return next.task.apply({}, next.args.concat(function() { | ||
if (!done) { | ||
done = true; | ||
_this.nbRunning--; | ||
_this._tryToRun(); | ||
return next.cb.apply({}, Array.prototype.slice.call(arguments, 0)); | ||
} | ||
})); | ||
}, wait)); | ||
return this._timeouts.push(setTimeout((function(_this) { | ||
return function() { | ||
return next.task.apply({}, next.args.concat(function() { | ||
var _ref; | ||
if (!done) { | ||
done = true; | ||
_this._nbRunning--; | ||
_this._tryToRun(); | ||
return (_ref = next.cb) != null ? _ref.apply({}, Array.prototype.slice.call(arguments, 0)) : void 0; | ||
} | ||
})); | ||
}; | ||
})(this), wait)); | ||
} | ||
@@ -42,3 +44,3 @@ }; | ||
task = arguments[0], args = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), cb = arguments[_i++]; | ||
this.queue.push({ | ||
this._queue.push({ | ||
task: task, | ||
@@ -51,5 +53,11 @@ args: args, | ||
Bottleneck.prototype.changeSettings = function(maxNb, minTime) { | ||
this.maxNb = maxNb != null ? maxNb : this.maxNb; | ||
this.minTime = minTime != null ? minTime : this.minTime; | ||
return this; | ||
}; | ||
Bottleneck.prototype.stopAll = function() { | ||
var a, _i, _len, _ref; | ||
_ref = this.timeouts; | ||
_ref = this._timeouts; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
@@ -56,0 +64,0 @@ a = _ref[_i]; |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.6.3 | ||
// Generated by CoffeeScript 1.7.1 | ||
(function() { | ||
@@ -3,0 +3,0 @@ module.exports = require("./Bottleneck"); |
{ | ||
"name": "bottleneck", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Async rate limiter", | ||
@@ -30,3 +30,3 @@ "main": "lib/index.js", | ||
"devDependencies":{ | ||
"coffee-script": "*", | ||
"coffee-script": "1.7.x", | ||
"browserify": "*", | ||
@@ -33,0 +33,0 @@ "uglify-js": "*" |
bottleneck | ||
========== | ||
Bottleneck is a very simple and efficient Asynchronous Rate Limiter for Node.JS. When dealing with services with limited resources, it's important to ensure that they don't become overloaded with requests. Bottleneck handles that case in a clean and simple way. | ||
Bottleneck is a simple and efficient Asynchronous Rate Limiter for Node.JS and the browser. When dealing with services with limited resources, it's important to ensure that they don't become overloaded. Bottleneck is the easiest solution as it doesn't add any complexity to the code. | ||
Databases, file systems, network access, APIs, etc. are all services that can easily be overwhelmed. | ||
#Installation | ||
#Install | ||
__Node__ | ||
@@ -23,5 +25,6 @@ ```javascript | ||
```javascript | ||
var Bottleneck = require("bottleneck"); //Ignore if Browser | ||
var Bottleneck = require("bottleneck"); //Node.JS only | ||
// Wait at least 2000ms between each request. Never more than 1 request running at a time. | ||
// Wait at least 2000ms between each request. | ||
// Never more than 1 request running at a time. | ||
var limiter = new Bottleneck(1, 2000); | ||
@@ -32,4 +35,4 @@ ``` | ||
* maxNb : How many requests can be running at the same time. 0 means unlimited. | ||
* minTime : How long to wait between each request. | ||
* maxNb : How many requests can be running at the same time. 0 for unlimited. | ||
* minTime : Optional. How long to wait after launching a request before launching another one. | ||
@@ -39,10 +42,12 @@ | ||
```javascript | ||
someAsyncCall(arg1, arg2, callback); | ||
someAsyncCall(arg1, arg2, argN, callback); | ||
``` | ||
You do | ||
```javascript | ||
limiter.submit(someAsyncCall, ar1, arg2, callback); | ||
limiter.submit(someAsyncCall, arg1, arg2, argN, callback); | ||
``` | ||
And now you can be assured that someAsyncCall will follow the rate guidelines! | ||
If a callback isn't necessary, pass ```null``` instead. | ||
###stopAll | ||
@@ -52,4 +57,8 @@ ```javascript | ||
``` | ||
stopAll cancels all queued up requests and prevents additonal requests from being submitted. | ||
Cancels all queued up requests and prevents additonal requests from being submitted. | ||
###changeSettings | ||
```javascript | ||
limiter.changeSettings(maxNb, minTime) | ||
``` | ||
Same parameters as the constructor, pass ```null``` to skip a parameter. |
Sorry, the diff of this file is not supported yet
12484
147
61