Skip to content

Commit

Permalink
Turn throttle into config option instead of instance property
Browse files Browse the repository at this point in the history
As was documented in command comments.

That allows to set global throttle as well as dynamically change
throttle command-wise
  • Loading branch information
samogot committed May 13, 2018
1 parent 14b1e60 commit 47f1975
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ function Osmosis(url, params) {
this.queue = new Queue(this);
this.command = new Command(this);
this.id = ++instanceId;
this.throttle = new RateLimiter(999, 1, true);
}


Expand All @@ -74,6 +73,7 @@ function Osmosis(url, params) {
* @property {string} accept - HTTP Accept header
* @property {bool} compressed - Compress HTTP requests
* @property {number} concurrency - Number of simultaneous HTTP requests
* @property {RateLimiter} throttle - RateLimiter object to throttle following requests
* @property {bool} decode_response - Decode compressed HTTP responses
* @property {number} follow - Number of redirects to follow
* @property {bool} follow_set_cookies - Set cookies for redirects
Expand All @@ -93,6 +93,7 @@ Osmosis.prototype.opts = {
'application/xml;q=0.9,*/*;q=0.8',
compressed: true,
concurrency: 5,
throttle: new RateLimiter(999, 1, true),
decode_response: true,
follow: 3,
follow_set_cookies: true,
Expand Down Expand Up @@ -182,7 +183,7 @@ Osmosis.prototype.request = function (url, opts, callback, tries) {
this.queue.requests++;
this.queue.push();

this.throttle.removeTokens(1, function(err, remainingRequests) {
opts.throttle.removeTokens(1, function(err, remainingRequests) {
request(url.method,
url,
url.params,
Expand Down Expand Up @@ -325,7 +326,7 @@ Osmosis.prototype.resources = function () {
'requests: ' + this.requests +
' (' + this.queue.requests + ' queued), ' +

'throttled: ' + parseInt(this.throttle.getTokensRemaining()) + ', ' +
'throttled: ' + parseInt(this.opts.throttle.getTokensRemaining()) + ', ' +

'RAM: ' + toMB(mem.rss) + ' (' + memDiff + '), ' +

Expand Down
4 changes: 3 additions & 1 deletion lib/commands/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
var RateLimiter = require('limiter').RateLimiter;

module.exports = function (tokensPerInterval, interval) {
this.instance.throttle = new RateLimiter(
var opts = this.getOpts();

opts.throttle = new RateLimiter(
tokensPerInterval || 1000,
interval || 1
);
Expand Down
85 changes: 85 additions & 0 deletions test/throttle.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var osmosis = require('../index'),
server = require('./server'),
RateLimiter = require('limiter').RateLimiter,
url = server.host + ':' + server.port;


Expand All @@ -26,6 +27,90 @@ module.exports.on = function (assert) {
});
};


module.exports.global_throttle = function (assert) {
var date0 = +Date.now(), interval = 2000;
osmosis.config('throttle', new RateLimiter(2, interval));
osmosis.get(url + '/get')
.then(function () {
var date = +Date.now();
assert.ok(date - date0 < interval);
})
.set({
test: osmosis
.get(url + '/get')
.then(function () {
var date = +Date.now();
assert.ok(date - date0 < interval);
})
.get(url + '/get')
.then(function () {
var date = +Date.now();
assert.ok(date - date0 >= interval);
})
})
.done(function () {
assert.done();
// Turn global throttling off, so it won't affect other tests
osmosis.config('throttle', new RateLimiter(999, 1));
});
};


module.exports.dynamic_change = function (assert) {
var date0 = +Date.now(), interval = 2000, date1, date2;
osmosis.get(url + '/get')
.then(function () {
var date = +Date.now();
assert.ok(date - date0 < interval);
})
.get(url + '/get')
.then(function () {
var date = +Date.now();
assert.ok(date - date0 < interval);
})
.get(url + '/get')
.then(function () {
var date = date1 = +Date.now();
assert.ok(date - date0 < interval);
})
.throttle(2, interval)
.get(url + '/get')
.then(function () {
var date = +Date.now();
assert.ok(date - date1 < interval);
})
.get(url + '/get')
.then(function () {
var date = +Date.now();
assert.ok(date - date1 < interval);
})
.get(url + '/get')
.then(function () {
var date = date2 = +Date.now();
assert.ok(date - date1 >= interval);
})
.throttle() // relax
.get(url + '/get')
.then(function () {
var date = +Date.now();
assert.ok(date - date2 < interval);
})
.get(url + '/get')
.then(function () {
var date = +Date.now();
assert.ok(date - date2 < interval);
})
.get(url + '/get')
.then(function () {
var date = +Date.now();
assert.ok(date - date2 < interval);
})
.done(function () {
assert.done();
});
};

module.exports.off = function (assert) {
var date0 = +Date.now(), interval = 2000;
osmosis.get(url + '/get')
Expand Down

0 comments on commit 47f1975

Please sign in to comment.