retry-request
Advanced tools
Comparing version 1.0.0 to 1.1.0
38
index.js
@@ -17,3 +17,3 @@ 'use strict'; | ||
module.exports = function (requestOpts, opts, callback) { | ||
function retryRequest(requestOpts, opts, callback) { | ||
var streamMode = typeof arguments[arguments.length - 1] !== 'function'; | ||
@@ -37,11 +37,19 @@ | ||
var retryStream = through(); | ||
var numAttempts = 0; | ||
var retryStream; | ||
var requestStream; | ||
var cacheStream; | ||
var cachedEvents = {}; | ||
var attempts = 0; | ||
if (streamMode) { | ||
retryStream = through(); | ||
} | ||
makeRequest(); | ||
if (streamMode) { | ||
return retryStream; | ||
} | ||
function resetStreams() { | ||
cachedEvents = {}; | ||
cacheStream = null; | ||
@@ -52,4 +60,4 @@ requestStream.abort(); | ||
function attempt() { | ||
attempts++; | ||
function makeRequest() { | ||
numAttempts++; | ||
@@ -83,3 +91,3 @@ if (streamMode) { | ||
// Send the response to see if we should try again. | ||
if (attempts <= opts.retries && opts.shouldRetryFn(response)) { | ||
if (numAttempts <= opts.retries && opts.shouldRetryFn(response)) { | ||
if (streamMode) { | ||
@@ -89,4 +97,3 @@ resetStreams(); | ||
attempt(); | ||
setTimeout(makeRequest, getNextRetryDelay(numAttempts)); | ||
return; | ||
@@ -106,5 +113,10 @@ } | ||
} | ||
} | ||
attempt(); | ||
return retryStream; | ||
}; | ||
module.exports = retryRequest; | ||
function getNextRetryDelay(retryNumber) { | ||
return (Math.pow(2, retryNumber) * 1000) + Math.floor(Math.random() * 1000); | ||
} | ||
module.exports.getNextRetryDelay = getNextRetryDelay; |
{ | ||
"name": "retry-request", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Retry a request.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha --timeout 5000" | ||
"test": "mocha --timeout 10000" | ||
}, | ||
@@ -9,0 +9,0 @@ "files": [ |
# retry-request | ||
> Retry a [request][request]. | ||
> Retry a [request][request] with built-in [exponential backoff](https://developers.google.com/analytics/devguides/reporting/core/v3/coreErrors#backoff). | ||
@@ -98,3 +98,3 @@ ## Use | ||
retryRequest(urlThatReturns404, function (err, resp, body) { | ||
retryRequest(urlThatReturns404, opts, function (err, resp, body) { | ||
// Your provided `request` instance was used. | ||
@@ -101,0 +101,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6704
92