Skip to content

Commit

Permalink
1.7.0 Chain()
Browse files Browse the repository at this point in the history
  • Loading branch information
SGrondin committed Mar 16, 2015
1 parent 7c47ad4 commit bad815e
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 76 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ limiter.incrementReservoir(incrementBy);

If `reservoir` reaches `0`, no new requests will be executed until it is no more `0`

###chain()

* `limiter` : If another limiter is passed, tasks that are ready to be executed will be submitted to that other limiter. *Default: `null` (none)*

Suppose you have 2 types of tasks, A and B. They both have their own limiter with their own settings, but both must also follow a global limiter C:
```javascript
var limiterA = new Bottleneck(...some settings...);
var limiterB = new Bottleneck(...some different settings...);
var limiterC = new Bottleneck(...some global settings...);
limiterA.chain(limiterC);
limiterB.chain(limiterC);
// Requests submitted to limiterA must follow the A and C rate limits.
// Requests submitted to limiterB must follow the B and C rate limits.
// Requests submitted to limiterC must follow the C rate limits.
```

##Execution guarantee

Expand Down
84 changes: 48 additions & 36 deletions bottleneck.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
(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);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.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.8.0
// Generated by CoffeeScript 1.9.1
(function() {
var Bottleneck,
__slice = [].slice;
slice = [].slice;

Bottleneck = (function() {
Bottleneck.strategy = Bottleneck.prototype.strategy = {
Expand All @@ -26,8 +26,14 @@
this.penalty = (15 * this.minTime) || 5000;
this.interrupt = false;
this.reservoir = null;
this.limiter = null;
}

Bottleneck.prototype.chain = function(limiter) {
this.limiter = limiter;
return this;
};

Bottleneck.prototype.check = function() {
return (this._nbRunning < this.maxNb || this.maxNb <= 0) && (this._nextRequest - Date.now()) <= 0 && ((this.reservoir == null) || this.reservoir > 0);
};
Expand All @@ -45,18 +51,24 @@
done = false;
index = -1 + this._timeouts.push(setTimeout((function(_this) {
return function() {
return next.task.apply({}, next.args.concat(function() {
var _ref;
var completed;
completed = function() {
var ref;
if (!done) {
done = true;
delete _this._timeouts[index];
_this._nbRunning--;
_this._tryToRun();
if (!_this.interrupt) {
return (_ref = next.cb) != null ? _ref.apply({}, Array.prototype.slice.call(arguments, 0)) : void 0;
return (ref = next.cb) != null ? ref.apply({}, Array.prototype.slice.call(arguments, 0)) : void 0;
}
}
}));
};
if (_this.limiter != null) {
return _this.limiter.submit.apply(_this.limiter, Array.prototype.concat.call(next.task, next.args, completed));
} else {
return next.task.apply({}, next.args.concat(completed));
}
};
})(this), wait));
return true;
Expand All @@ -66,8 +78,8 @@
};

Bottleneck.prototype.submit = function() {
var args, cb, reachedHighWaterMark, task, _i;
task = arguments[0], args = 3 <= arguments.length ? __slice.call(arguments, 1, _i = arguments.length - 1) : (_i = 1, []), cb = arguments[_i++];
var args, cb, i, reachedHighWaterMark, task;
task = arguments[0], args = 3 <= arguments.length ? slice.call(arguments, 1, i = arguments.length - 1) : (i = 1, []), cb = arguments[i++];
reachedHighWaterMark = this.highWater > 0 && this._queue.length === this.highWater;
if (this.strategy === Bottleneck.prototype.strategy.BLOCK && (reachedHighWaterMark || this._unblockTime >= Date.now())) {
this._unblockTime = Date.now() + this.penalty;
Expand Down Expand Up @@ -119,11 +131,11 @@
};

Bottleneck.prototype.stopAll = function(interrupt) {
var a, _i, _len, _ref;
var a, i, len, ref;
this.interrupt = interrupt != null ? interrupt : this.interrupt;
_ref = this._timeouts;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
a = _ref[_i];
ref = this._timeouts;
for (i = 0, len = ref.length; i < len; i++) {
a = ref[i];
clearTimeout(a);
}
this._tryToRun = function() {};
Expand All @@ -144,59 +156,59 @@
}).call(this);

},{"./Cluster":2}],2:[function(require,module,exports){
// Generated by CoffeeScript 1.8.0
// Generated by CoffeeScript 1.9.1
(function() {
var Cluster,
__hasProp = {}.hasOwnProperty;
hasProp = {}.hasOwnProperty;

Cluster = (function() {
function Cluster(maxNb, minTime, highWater, strategy) {
var _base;
var base;
this.maxNb = maxNb;
this.minTime = minTime;
this.highWater = highWater;
this.strategy = strategy;
this.limiters = {};
this.Bottleneck = require("./Bottleneck");
if (typeof (_base = setInterval((function(_this) {
if (typeof (base = setInterval((function(_this) {
return function() {
var k, time, v, _ref, _results;
var k, ref, results, time, v;
time = Date.now();
_ref = _this.limiters;
_results = [];
for (k in _ref) {
v = _ref[k];
ref = _this.limiters;
results = [];
for (k in ref) {
v = ref[k];
if ((v._nextRequest + (60 * 1000 * 5)) < time) {
_results.push(delete _this.limiters[k]);
results.push(delete _this.limiters[k]);
} else {
_results.push(void 0);
results.push(void 0);
}
}
return _results;
return results;
};
})(this), 60 * 1000)).unref === "function") {
_base.unref();
base.unref();
}
}

Cluster.prototype.key = function(key) {
var _ref;
var ref;
if (key == null) {
key = "";
}
return (_ref = this.limiters[key]) != null ? _ref : (this.limiters[key] = new this.Bottleneck(this.maxNb, this.minTime, this.highWater, this.strategy));
return (ref = this.limiters[key]) != null ? ref : (this.limiters[key] = new this.Bottleneck(this.maxNb, this.minTime, this.highWater, this.strategy));
};

Cluster.prototype.all = function(cb) {
var k, v, _ref, _results;
_ref = this.limiters;
_results = [];
for (k in _ref) {
if (!__hasProp.call(_ref, k)) continue;
v = _ref[k];
_results.push(cb(v));
var k, ref, results, v;
ref = this.limiters;
results = [];
for (k in ref) {
if (!hasProp.call(ref, k)) continue;
v = ref[k];
results.push(cb(v));
}
return _results;
return results;
};

Cluster.prototype.keys = function() {
Expand All @@ -213,7 +225,7 @@

},{"./Bottleneck":1}],3:[function(require,module,exports){
(function (global){
// Generated by CoffeeScript 1.8.0
// Generated by CoffeeScript 1.9.1
(function() {
module.exports = require("./Bottleneck");

Expand Down
2 changes: 1 addition & 1 deletion bottleneck.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bottleneck",
"main": "bottleneck.js",
"version": "1.6.0",
"version": "1.7.0",
"homepage": "https://github.com/SGrondin/bottleneck",
"authors": [
"SGrondin <github@simongrondin.name>"
Expand Down
Loading

0 comments on commit bad815e

Please sign in to comment.