Skip to content

Commit

Permalink
Merge pull request #1 from mastrzyz/mastrzyz/network_retries
Browse files Browse the repository at this point in the history
Added handling for when we have network failures in the retry mechanism
  • Loading branch information
mastrzyz authored Jun 29, 2020
2 parents f8d4424 + 4903287 commit 756669b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions lib/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export enum HttpCodes {

const HttpRedirectCodes: number[] = [HttpCodes.MovedPermanently, HttpCodes.ResourceMoved, HttpCodes.SeeOther, HttpCodes.TemporaryRedirect, HttpCodes.PermanentRedirect];
const HttpResponseRetryCodes: number[] = [HttpCodes.BadGateway, HttpCodes.ServiceUnavailable, HttpCodes.GatewayTimeout];
const NetworkRetryErrors: string[] = ['ECONNRESET', 'ENOTFOUND', 'ESOCKETTIMEDOUT', 'ETIMEDOUT', 'ECONNREFUSED'];
const RetryableHttpVerbs: string[] = ['OPTIONS', 'GET', 'DELETE', 'HEAD'];
const ExponentialBackoffCeiling = 10;
const ExponentialBackoffTimeSlice = 5;
Expand Down Expand Up @@ -243,8 +244,15 @@ export class HttpClient implements ifm.IHttpClient {

let response: HttpClientResponse;
while (numTries < maxTries) {
response = await this.requestRaw(info, data);

try{
response = await this.requestRaw(info, data);
}
catch (err) {
if(err && err.code && NetworkRetryErrors.indexOf(err.code) !== 1){
continue
}
throw err;
}
// Check if it's an authentication challenge
if (response && response.message && response.message.statusCode === HttpCodes.Unauthorized) {
let authenticationHandler: ifm.IRequestHandler;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typed-rest-client",
"version": "1.7.3",
"version": "1.8.3",
"description": "Node Rest and Http Clients for use with TypeScript",
"main": "./RestClient.js",
"scripts": {
Expand Down

0 comments on commit 756669b

Please sign in to comment.