Concurrent Request
Simple request pooling with backoff strategies and jitter.
Usage
const Pool = const opts = Math * 1000 // exponentional backoff jitter: 1000 // 1 second range of jitter (+/- .5 seconds) size: 5 // At most 5 active connections tries: 5 // Give up after retrying 5 times { ife return ; // Retry if connection fails ifrespcode === 429 return ; // retry return ; // The request succeeded }var request = opts; ;
API
var request = new Pool(opts)
Create a new pool of request handlers. This pool will limit the number of concurrent requests made through the pool, along with retrying failed requests with backoff and jitter, making your application super friendly to remote APIs.
opts
is required and is an object of the form:
jitter: Number // See below for more information size: Number // The maximum number of concurrent requests tries: Number // interval: Function // See below for more information handler: Function // See below for more information
interval
- A function that computes the number of milliseconds to wait before retrying a request. For example(count) => Math.pow(2, count) * 1000
would retry after 2 seconds, then 4, then 8, then 16, and so on. This defaults to() => 0
(retry immediately).jitter
- A number that determines the amount of random jitter to apply to the retry interval. If you specify1000
for example, you will end up with +/- 500ms. The reason for this is to prevent allsize
of your concurrent requests from failing and retrying at the same exact time putting strain on the API endpoint you are hitting. The algorithm used to compute the jitter is:interval + jitter * (Math.random() - .5)
. This value will default to0
(which means no jitter).size
- The maximum number of concurrent requests. Defaults toInfinity
.handler
- A function that determines when the library should attempt to retry. This method signature should be:function (e, resp, body, cb)
wheree
,resp
, andbody
are the values received fromrequest
and thecb
is an error first callback. If you invoke the callback with an error, the library will retry the request. Otherwise it will treat the request as succesful. Defaults to() => cb(e)
tries
- The number of times this library should re-attempt a request. Defaults to0
.
request(opts, function callback(e, resp, body) {})
This method accepts an opts
object, which is the same exact opts
accepted by the request module itself.
When invoked, this function will queue the request. When the number of concurrent requests drops below the size
threshold specified in the constructor, the library will work through this queue.
Once the request has either failed tries
times or completed, callback
will be invoked. If the request failed, e
will be an array of all errors returned by the handler. Otherwise resp
and body
will be the same as you would get from the request
library.
Note: we do not support any other way of invoking the underlying
request
object at this time. Streams are not supported.