Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements throttling #210

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
'use strict';

var Command = require('./lib/Command.js'),
Queue = require('./lib/Queue.js'),
request = require('./lib/Request.js'),
libxml = require('libxmljs-dom'),
var Command = require('./lib/Command.js'),
Queue = require('./lib/Queue.js'),
request = require('./lib/Request.js'),
libxml = require('libxmljs-dom'),
RateLimiter = require('limiter').RateLimiter,
instanceId = 0,
memoryUsage = 0,
cachedSelectors = {},
Expand Down Expand Up @@ -58,9 +59,9 @@ function Osmosis(url, params) {
return Osmosis.get(url, params);
}

this.queue = new Queue(this);
this.command = new Command(this);
this.id = ++instanceId;
this.queue = new Queue(this);
this.command = new Command(this);
this.id = ++instanceId;
}


Expand All @@ -72,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 @@ -91,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 @@ -147,7 +150,7 @@ Osmosis.prototype.config = function (option, value) {

/**
* Run (or re-run) an Osmosis instance.
*g
*
* If you frequently use the same Osmosis instance
* (such as in an Express server), it's much more efficient to
* initialize the instance once and repeatedly use `run` as needed.
Expand Down Expand Up @@ -184,6 +187,7 @@ Osmosis.prototype.request = function (url, opts, callback, tries) {
opts.user_agent = opts.user_agent();
}

opts.throttle.removeTokens(1, function(err, remainingRequests) {
request(url.method,
url,
url.params,
Expand Down Expand Up @@ -229,6 +233,7 @@ Osmosis.prototype.request = function (url, opts, callback, tries) {
href + ' -> ' + new_url);
}
});
});
};

/**
Expand Down Expand Up @@ -320,19 +325,21 @@ Osmosis.prototype.resources = function () {
}

this.command.debug(
'stack: ' + this.queue.count + ', ' +
'stack: ' + this.queue.count + ', ' +

'requests: ' + this.requests +
' (' + this.queue.requests + ' queued), ' +

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

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

'libxml: ' + ((libxml_mem / mem.rss) * 100).toFixed(1) +
'% (' + nodes + ' nodes), ' +
'libxml: ' + ((libxml_mem / mem.rss) * 100).toFixed(1) +
'% (' + nodes + ' nodes), ' +

'heap: ' + ((mem.heapUsed / mem.heapTotal) * 100)
.toFixed(0) + '% of ' +
toMB(mem.heapTotal)
'heap: ' + ((mem.heapUsed / mem.heapTotal) * 100)
.toFixed(0) + '% of ' +
toMB(mem.heapTotal)
);

memoryUsage = mem.rss;
Expand Down
24 changes: 24 additions & 0 deletions lib/commands/throttle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Set a throttle. Short for `.config({ throttle: ... })`
*
* @function throttle
* @memberof Command
* @param {Number} tokensPerInterval Maximum number of tokens that can be
* removed at any given moment and over the course of one interval.
* @param {String|Number} interval The interval length in milliseconds, or as
* one of the following strings: 'second', 'minute', 'hour', day'.
* @see Osmosis.config
*/

var RateLimiter = require('limiter').RateLimiter;

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

opts.throttle = new RateLimiter(
tokensPerInterval || 1000,
interval || 1
);

return this;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"libxmljs-dom": "~0.0.11",
"limiter": "^1.1.0",
"needle": "^1.6.0"
},
"devDependencies": {
Expand Down
135 changes: 135 additions & 0 deletions test/throttle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
var osmosis = require('../index'),
server = require('./server'),
RateLimiter = require('limiter').RateLimiter,
url = server.host + ':' + server.port;


module.exports.on = function (assert) {
var date0 = +Date.now(), interval = 2000;
osmosis.get(url + '/get')
.throttle(2, interval)
.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 = +Date.now();
assert.ok(date - date0 >= interval);
})
.done(function () {
assert.done();
});
};


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')
.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 = +Date.now();
assert.ok(date - date0 < interval);
})
.done(function () {
assert.done();
});
};