Skip to content

Commit

Permalink
feature(throttle): implements throttling requests
Browse files Browse the repository at this point in the history
  • Loading branch information
JonnyBGod authored and samogot committed Nov 13, 2018
1 parent 5bf419b commit 4b96309
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
22 changes: 14 additions & 8 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,10 @@ 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;
this.throttle = new RateLimiter(999, 1, true);
}


Expand Down Expand Up @@ -147,7 +149,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 +186,7 @@ Osmosis.prototype.request = function (url, opts, callback, tries) {
opts.user_agent = opts.user_agent();
}

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

/**
Expand Down Expand Up @@ -325,6 +329,8 @@ Osmosis.prototype.resources = function () {
'requests: ' + this.requests +
' (' + this.queue.requests + ' queued), ' +

'tokens: ' + parseInt(this.throttle.getTokensRemaining()) + ', ' +

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

'libxml: ' + ((libxml_mem / mem.rss) * 100).toFixed(1) +
Expand Down
22 changes: 22 additions & 0 deletions lib/commands/throttle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* 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) {
this.instance.throttle = new RateLimiter(
tokensPerInterval || 1000,
interval || 1
);

return this;
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "osmosis",
"version": "1.1.8",
"version": "1.1.6",
"description": "Web scraper for NodeJS",
"keywords": [
"web",
Expand All @@ -21,6 +21,7 @@
},
"dependencies": {
"libxmljs-dom": "~0.0.11",
"limiter": "^1.1.0",
"needle": "^1.6.0"
},
"devDependencies": {
Expand Down

0 comments on commit 4b96309

Please sign in to comment.