throttled-request
Node.js module to easily throttle HTTP requests.
How it works
This tool was made to work with the popular request module, which simplifies the HTTP requests in Node.js. Therefore, this must be consireded a wrapper around request.
First, you instantiate a throttledRequest instance by passing a request function, which is going to act as the requester - you still need to $npm install request
independently. - After this you can configure the throttle rate (number of requests / time), then you're able to use throttled-request to perform your HTTP requests.
Installation
Install it using npm
$ npm install throttled-request
Usage
First, you must set it up:
var request = throttledRequest = request; throttledRequest;//This will throttle the requests so no more than 5 are made every second
Or you may use a configurable throttle by providing a function that returns the next delay, in milliseconds:
var request = throttledRequest = request; throttledRequest;//This will throttle the requests so no more than 1 is made every 5 to 15 seconds (random delay)
Then you can use throttledRequest
just as you use request: passing a callback, or as a stream.
Passing a callback
;
As a stream
;
request
event
The throttledRequest
emits a request
event just after each actual request is made.
Full example
var request = throttledRequest = request startedAt = Date; throttledRequest; throttledRequest; //Throttle 10 requests in parallelfor var i = 0; i < 10; i++ ; /*Output:Making a request. Elapsed time: 3 msMaking a request. Elapsed time: 5 msGot response. Elapsed time: 488 msGot response. Elapsed time: 509 msMaking a request. Elapsed time: 1002 msMaking a request. Elapsed time: 1003 msGot response. Elapsed time: 1450 msGot response. Elapsed time: 1513 msMaking a request. Elapsed time: 2003 msMaking a request. Elapsed time: 2003 msGot response. Elapsed time: 2431 msGot response. Elapsed time: 2470 msMaking a request. Elapsed time: 3004 msMaking a request. Elapsed time: 3005 msGot response. Elapsed time: 3446 msGot response. Elapsed time: 3451 msMaking a request. Elapsed time: 4007 msMaking a request. Elapsed time: 4007 msGot response. Elapsed time: 4440 msGot response. Elapsed time: 4783 ms*/
Can I use everything that comes with request?
No, there's some things you can't use. For example, the shortcut functions .get
, .post
, .put
, etc. are not available. If you'd like to have them, this is a great opportunity to contribute!
Running tests
Run the tests with npm
$ npm test