Skip to content

Commit

Permalink
Also retry on HTTP Status 429 "Too Many Requests"
Browse files Browse the repository at this point in the history
API providers may return an HTTP status code of 429 to mean "Too Many
Requests".

By setting a custom retry policy, this module can be used to wait
until the throttling period has expired and try again.

Reference: https://httpstatuses.com/429
  • Loading branch information
markstos committed Sep 18, 2018
1 parent 65e5a68 commit bf447d7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

![NPM](https://nodei.co/npm/requestretry.png?downloadRank=true) ![NPM](https://nodei.co/npm-dl/requestretry.png?months=3&height=2)

When the connection fails with one of `ECONNRESET`, `ENOTFOUND`, `ESOCKETTIMEDOUT`, `ETIMEDOUT`, `ECONNREFUSED`, `EHOSTUNREACH`, `EPIPE`, `EAI_AGAIN` or when an HTTP 5xx error occurrs, the request will automatically be re-attempted as these are often recoverable errors and will go away on retry.
When the connection fails with one of `ECONNRESET`, `ENOTFOUND`, `ESOCKETTIMEDOUT`, `ETIMEDOUT`, `ECONNREFUSED`, `EHOSTUNREACH`, `EPIPE`, `EAI_AGAIN` or when an HTTP 5xx or 429 error occurrs, the request will automatically be re-attempted as these are often recoverable errors and will go away on retry.


> ## ❤️ Shameless plug
Expand Down
9 changes: 8 additions & 1 deletion strategies/HTTPError.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,12 @@
* @return {Boolean} true if the request had a recoverable HTTP error
*/
module.exports = function HTTPError(err, response) {
return response && 500 <= response.statusCode && response.statusCode < 600;
let statusCode;

if (response) {
statusCode = response.statusCode
}

// 429 means "Too Many Requests" while 5xx means "Server Error"
return statusCode && (statusCode === 429 || (500 <= statusCode && statusCode < 600));
};
2 changes: 1 addition & 1 deletion test/strategies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function checkHTTPErrors(strategy) {
}), code + ' error is not recoverable');
});

[500, 599].forEach(function (code) {
[429, 500, 599].forEach(function (code) {
t.ok(strategy(null, {
statusCode: code
}), code + ' error is recoverable');
Expand Down

0 comments on commit bf447d7

Please sign in to comment.