This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add retry with backoff to requests
This adds a bit more tolerance to network outages etc. If a 40x status code is recieved from the API it will not retry as this indicates a bad request – so no need to keep sening the same request. There were no end-to-end tests predating this feature so I added some. Fixes bugsnag/webpack-bugsnag-plugins#17.
- Loading branch information
1 parent
511f83d
commit 5b1ad55
Showing
9 changed files
with
540 additions
and
18 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,32 @@ | ||
const request = require('request') | ||
const Backoff = require('backo') | ||
const MAX_ATTEMPTS = 5 | ||
const MIN_BACKOFF_INTERVAL = process.env.BUGSNAG_MIN_BACKOFF_INTERVAL || 500 | ||
const MAX_BACKOFF_INTERVAL = process.env.BUGSNAG_MAX_BACKOFF_INTERVAL || 5000 | ||
|
||
module.exports = (url, formData) => { | ||
return new Promise((resolve, reject) => { | ||
const backoff = new Backoff({ min: MIN_BACKOFF_INTERVAL, max: MAX_BACKOFF_INTERVAL }) | ||
|
||
const onSuccess = () => resolve() | ||
const onError = (err) => { | ||
if (backoff.attempts >= MAX_ATTEMPTS - 1) return reject(err) | ||
if (err.statusCode >= 400 && err.statusCode < 500) return reject(err) | ||
setTimeout(() => req(url, formData, { onSuccess, onError }), backoff.duration()) | ||
} | ||
|
||
req(url, formData, { onError, onSuccess }) | ||
}) | ||
} | ||
|
||
const req = (url, formData, { onError, onSuccess }) => { | ||
request.post({ url, formData }, function (err, res, body) { | ||
if (err || res.statusCode !== 200) { | ||
err = err || new Error(`${res.statusMessage} (${res.statusCode}) - ${body}`) | ||
if (res.statusCode) err.statusCode = res.statusCode | ||
onError(err) | ||
} else { | ||
onSuccess() | ||
} | ||
}) | ||
} |
Oops, something went wrong.