Skip to content

Commit

Permalink
1.13.0 CS 1.11.0, rejectOnDrop, more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simon committed Sep 25, 2016
1 parent 7f8e028 commit ad2d7fd
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 117 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ or
Most APIs have a rate limit. For example, the Reddit.com API limits programs to 1 request every 2 seconds.

```js
var Bottleneck = require("bottleneck"); //Node only
var Bottleneck = require("bottleneck"); // Skip when browser side

// Never more than 1 request running at a time.
// Wait at least 2000ms between each request.
Expand Down Expand Up @@ -114,7 +114,7 @@ It's also possible to replace the Promise library used:
var Bottleneck = require("bottleneck");
Bottleneck.prototype.Promise = myPromiseLibrary;

var limiter = new Bottleneck(maxConcurrent, minTime, highWater, strategy);
var limiter = new Bottleneck(maxConcurrent, minTime, highWater, strategy, rejectOnDrop);
```


Expand Down
101 changes: 57 additions & 44 deletions bottleneck.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(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.10.0
// Generated by CoffeeScript 1.11.0
(function() {
var Bottleneck, MIDDLE_PRIORITY, NB_PRIORITIES,
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
Expand All @@ -24,11 +24,10 @@
Bottleneck.DLList = Bottleneck.prototype.DLList = require("./DLList");

Bottleneck.Promise = Bottleneck.prototype.Promise = (function() {
var error1;
try {
return require("bluebird");
} catch (error1) {
e = error1;
} catch (error) {
e = error;
return typeof Promise !== "undefined" && Promise !== null ? Promise : function() {
throw new Error("Bottleneck: install 'bluebird' or use Node 0.12 or higher for Promise support");
};
Expand All @@ -47,7 +46,7 @@
this._nextRequest = Date.now();
this._nbRunning = 0;
this._queues = this._makeQueues();
this._timeouts = [];
this._running = [];
this._unblockTime = 0;
this.penalty = (15 * this.minTime) || 5000;
this.interrupt = false;
Expand All @@ -57,10 +56,8 @@
}

Bottleneck.prototype._trigger = function(name, args) {
var dropped;
if (name === "dropped" && this.rejectOnDrop) {
dropped = args[0];
dropped.cb(new Error("This job has been dropped."));
if (this.rejectOnDrop && name === "dropped") {
args[0].cb.apply({}, [{}, new Error("This job has been dropped by Bottleneck")]);
}
return setTimeout(((function(_this) {
return function() {
Expand Down Expand Up @@ -148,31 +145,34 @@
this._nextRequest = Date.now() + wait + this.minTime;
next = (this._getFirst(this._queues)).shift();
if (queued === 1) {
this._trigger('empty', []);
this._trigger("empty", []);
}
done = false;
index = -1 + this._timeouts.push(setTimeout((function(_this) {
return function() {
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;
index = -1 + this._running.push({
timeout: setTimeout((function(_this) {
return function() {
var completed;
completed = function() {
var ref;
if (!done) {
done = true;
delete _this._running[index];
_this._nbRunning--;
_this._tryToRun();
if (!_this.interrupt) {
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(next.task, next.args, completed));
} else {
return next.task.apply({}, next.args.concat(completed));
}
};
if (_this.limiter != null) {
return _this.limiter.submit.apply(_this.limiter, Array.prototype.concat(next.task, next.args, completed));
} else {
return next.task.apply({}, next.args.concat(completed));
}
};
})(this), wait));
})(this), wait),
job: next
});
return true;
} else {
return false;
Expand All @@ -199,12 +199,12 @@
this._unblockTime = Date.now() + this.penalty;
this._nextRequest = this._unblockTime + this.minTime;
this._queues = this._makeQueues();
this._trigger('dropped', [job]);
this._trigger("dropped", [job]);
return true;
} else if (reachedHighWaterMark) {
shifted = this.strategy === Bottleneck.prototype.strategy.LEAK ? (this._getFirst(this._queues.slice(priority).reverse())).shift() : this.strategy === Bottleneck.prototype.strategy.OVERFLOW_PRIORITY ? (this._getFirst(this._queues.slice(priority + 1).reverse())).shift() : this.strategy === Bottleneck.prototype.strategy.OVERFLOW ? job : void 0;
if (shifted != null) {
this._trigger('dropped', [shifted]);
this._trigger("dropped", [shifted]);
}
if ((shifted == null) || this.strategy === Bottleneck.prototype.strategy.OVERFLOW) {
return reachedHighWaterMark;
Expand Down Expand Up @@ -238,19 +238,20 @@
return new Bottleneck.prototype.Promise((function(_this) {
return function(resolve, reject) {
return _this.submitPriority.apply({}, Array.prototype.concat(priority, wrapped, function() {
var args, error;
error = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
return (error != null ? reject : resolve).apply({}, args);
var args, failed;
failed = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];
return (failed != null ? reject : resolve).apply({}, args);
}));
};
})(this));
};

Bottleneck.prototype.changeSettings = function(maxNb, minTime, highWater, strategy) {
Bottleneck.prototype.changeSettings = function(maxNb, minTime, highWater, strategy, rejectOnDrop) {
this.maxNb = maxNb != null ? maxNb : this.maxNb;
this.minTime = minTime != null ? minTime : this.minTime;
this.highWater = highWater != null ? highWater : this.highWater;
this.strategy = strategy != null ? strategy : this.strategy;
this.rejectOnDrop = rejectOnDrop != null ? rejectOnDrop : this.rejectOnDrop;
while (this._tryToRun()) {}
return this;
};
Expand Down Expand Up @@ -284,17 +285,28 @@
};

Bottleneck.prototype.stopAll = function(interrupt) {
var a, j, len, ref;
var a, j, job, k, len, len1, ref, ref1;
this.interrupt = interrupt != null ? interrupt : this.interrupt;
ref = this._timeouts;
ref = this._running;
for (j = 0, len = ref.length; j < len; j++) {
a = ref[j];
clearTimeout(a);
clearTimeout(a.timeout);
}
this._tryToRun = function() {};
return this.check = this.submit = this.submitPriority = this.schedule = this.schedulePriority = function() {
this.check = this.submit = this.submitPriority = this.schedule = this.schedulePriority = function() {
return false;
};
if (this.interrupt) {
ref1 = this._running;
for (k = 0, len1 = ref1.length; k < len1; k++) {
a = ref1[k];
this._trigger("dropped", [a.job]);
}
}
while (job = (this._getFirst(this._queues)).shift()) {
this._trigger("dropped", [job]);
}
return this._trigger("empty", []);
};

return Bottleneck;
Expand All @@ -306,17 +318,18 @@
}).call(this);

},{"./Cluster":2,"./DLList":3,"bluebird":undefined}],2:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
// Generated by CoffeeScript 1.11.0
(function() {
var Cluster,
hasProp = {}.hasOwnProperty;

Cluster = (function() {
function Cluster(maxNb, minTime, highWater, strategy) {
function Cluster(maxNb, minTime, highWater, strategy, rejectOnDrop) {
this.maxNb = maxNb;
this.minTime = minTime;
this.highWater = highWater;
this.strategy = strategy;
this.rejectOnDrop = rejectOnDrop;
this.limiters = {};
this.Bottleneck = require("./Bottleneck");
this.startAutoCleanup();
Expand All @@ -327,7 +340,7 @@
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, this.rejectOnDrop));
};

Cluster.prototype.deleteKey = function(key) {
Expand Down Expand Up @@ -388,7 +401,7 @@
}).call(this);

},{"./Bottleneck":1}],3:[function(require,module,exports){
// Generated by CoffeeScript 1.10.0
// Generated by CoffeeScript 1.11.0
(function() {
var DLList;

Expand Down Expand Up @@ -447,7 +460,7 @@

},{}],4:[function(require,module,exports){
(function (global){
// Generated by CoffeeScript 1.10.0
// Generated by CoffeeScript 1.11.0
(function() {
module.exports = require("./Bottleneck");

Expand Down
Loading

0 comments on commit ad2d7fd

Please sign in to comment.