forked from AirVantage/zuorajs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from CoSchedule/jfm-add_request_retry_feature
[CHORE] Add ability to internally retry /oauth/token calls.
- Loading branch information
Showing
2 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const requestPromise = require('request-promise'); | ||
const requestPromiseErrors = require('request-promise/errors'); | ||
|
||
const requestWithRetries = async (requestOptions, shouldRetry, computeRetryDelay) => { | ||
const makeRequest = async (tryCount) => { | ||
let response; | ||
try { | ||
response = await requestPromise(requestOptions); | ||
} catch (err) { | ||
if (!shouldRetry || !shouldRetry(err, tryCount)) { | ||
throw err; | ||
} | ||
|
||
const nextTryCount = tryCount + 1; | ||
const retryDelay = await ((computeRetryDelay && computeRetryDelay(err, nextTryCount)) || 1000); | ||
await new Promise((resolve) => setTimeout(resolve, retryDelay)); | ||
|
||
response = makeRequest(nextTryCount); | ||
} | ||
|
||
return response; | ||
}; | ||
|
||
return makeRequest(1); | ||
}; | ||
|
||
const isRequestError = (err) => (err instanceof requestPromiseErrors.RequestError); | ||
const isStatusCodeError = (err) => (err instanceof requestPromiseErrors.StatusCodeError); | ||
|
||
module.exports = { | ||
requestWithRetries, | ||
isRequestError, | ||
isStatusCodeError, | ||
}; |