Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom retry codes configuration options (#1569) #1570

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/http-client/__tests__/auth.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as httpm from '../lib'
import * as am from '../lib/auth'
import * as httpm from '../src/index'
import * as am from '../src/auth'

describe('auth', () => {
beforeEach(() => {})
Expand Down
2 changes: 1 addition & 1 deletion packages/http-client/__tests__/basics.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import * as httpm from '..'
import * as httpm from '../src/index'
import * as path from 'path'
import * as fs from 'fs'

Expand Down
2 changes: 1 addition & 1 deletion packages/http-client/__tests__/headers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import * as httpm from '..'
import * as httpm from '../src/index'

describe('headers', () => {
let _http: httpm.HttpClient
Expand Down
2 changes: 1 addition & 1 deletion packages/http-client/__tests__/keepalive.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as httpm from '../lib'
import * as httpm from '../src/index'

describe('basics', () => {
let _http: httpm.HttpClient
Expand Down
4 changes: 2 additions & 2 deletions packages/http-client/__tests__/proxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import * as http from 'http'
import * as httpm from '../lib/'
import * as pm from '../lib/proxy'
import * as httpm from '../src/index'
import * as pm from '../src/proxy'
import {ProxyAgent} from 'undici'
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
const proxy = require('proxy')
Expand Down
98 changes: 98 additions & 0 deletions packages/http-client/__tests__/retry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import * as httpm from '../src/index'

describe('basics', () => {
let _http: httpm.HttpClient

beforeEach(() => {
_http = new httpm.HttpClient('http-client-tests', undefined, {
allowRetries: true,
maxRetries: 5,
retryCodes: [404, 500, 502],
noRetryCodes: [403, 404, 504]
})
})

afterEach(() => {})

it('constructs', () => {
const http: httpm.HttpClient = new httpm.HttpClient('thttp-client-tests')
expect(http).toBeDefined()
})

it('no retry on error code 400 (not given in options)', async () => {
const res: httpm.HttpClientResponse = await _http.get(
`https://postman-echo.com/redirect-to?url=${encodeURIComponent(
'https://postman-echo.com/status/400'
)}&status_code=400`
)

expect(res.retryCount).toBe(undefined)
expect(res.message.statusCode).toBe(400)
})

it('no retry on error code 403 (noRetryOnCodes)', async () => {
const res: httpm.HttpClientResponse = await _http.get(
`https://postman-echo.com/redirect-to?url=${encodeURIComponent(
'https://postman-echo.com/status/403'
)}&status_code=403`
)

expect(res.retryCount).toBe(undefined)
expect(res.message.statusCode).toBe(403)
})

it('retry on error code 404 (retryOnCodes used over noRetryOnCode)', async () => {
const res: httpm.HttpClientResponse = await _http.get(
`https://postman-echo.com/redirect-to?url=${encodeURIComponent(
'https://postman-echo.com/status/404'
)}&status_code=404`
)

expect(res.retryCount).toBe(5)
expect(res.message.statusCode).toBe(404)
})

it('retry on error code 500 (retryOnCodes only)', async () => {
const res: httpm.HttpClientResponse = await _http.get(
`https://postman-echo.com/redirect-to?url=${encodeURIComponent(
'https://postman-echo.com/status/500'
)}&status_code=500`
)

expect(res.retryCount).toBe(5)
expect(res.message.statusCode).toBe(500)
})

it('retry on error code 502 (retryOnCodes and HttpResponseRetryCodes)', async () => {
const res: httpm.HttpClientResponse = await _http.get(
`https://postman-echo.com/redirect-to?url=${encodeURIComponent(
'https://postman-echo.com/status/502'
)}&status_code=502`
)

expect(res.retryCount).toBe(5)
expect(res.message.statusCode).toBe(502)
})

it('retry on error code 503 (HttpResponseRetryCodes only)', async () => {
const res: httpm.HttpClientResponse = await _http.get(
`https://postman-echo.com/redirect-to?url=${encodeURIComponent(
'https://postman-echo.com/status/503'
)}&status_code=503`
)

expect(res.retryCount).toBe(5)
expect(res.message.statusCode).toBe(503)
})

it('no retry on error code 504 (noRetryOnCodes used over HttpResponseRetryCodes)', async () => {
const res: httpm.HttpClientResponse = await _http.get(
`https://postman-echo.com/redirect-to?url=${encodeURIComponent(
'https://postman-echo.com/status/504'
)}&status_code=504`
)

expect(res.retryCount).toBe(undefined)
expect(res.message.statusCode).toBe(504)
})
})
31 changes: 29 additions & 2 deletions packages/http-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ const HttpRedirectCodes: number[] = [
HttpCodes.TemporaryRedirect,
HttpCodes.PermanentRedirect
]
const HttpResponseRetryCodes: number[] = [
const DefaultHttpResponseRetryCodes: number[] = [
HttpCodes.BadGateway,
HttpCodes.ServiceUnavailable,
HttpCodes.GatewayTimeout
Expand All @@ -90,6 +90,8 @@ export class HttpClientResponse {
}

message: http.IncomingMessage
retryCount: number | undefined

async readBody(): Promise<string> {
return new Promise<string>(async resolve => {
let output = Buffer.alloc(0)
Expand Down Expand Up @@ -141,6 +143,8 @@ export class HttpClient {
private _proxyAgentDispatcher: any
private _keepAlive = false
private _disposed = false
private _retryOnCodes: number[] = []
private _noRetryOnCodes: number[] = []

constructor(
userAgent?: string,
Expand Down Expand Up @@ -180,6 +184,14 @@ export class HttpClient {
if (requestOptions.maxRetries != null) {
this._maxRetries = requestOptions.maxRetries
}

if (requestOptions.retryCodes != null) {
this._retryOnCodes = requestOptions.retryCodes
}

if (requestOptions.noRetryCodes != null) {
this._noRetryOnCodes = requestOptions.noRetryCodes
}
}
}

Expand Down Expand Up @@ -435,9 +447,10 @@ export class HttpClient {

if (
!response.message.statusCode ||
!HttpResponseRetryCodes.includes(response.message.statusCode)
!this._shouldRetryOnCode(response.message.statusCode)
) {
// If not a retry code, return immediately instead of retrying
response.retryCount = numTries - 1
return response
}

Expand All @@ -449,6 +462,7 @@ export class HttpClient {
}
} while (numTries < maxTries)

response.retryCount = numTries - 1
return response
}

Expand Down Expand Up @@ -823,6 +837,19 @@ export class HttpClient {
}
})
}

private _shouldRetryOnCode(statusCode: number): boolean {
if (this._retryOnCodes.includes(statusCode)) {
return true
} else if (
DefaultHttpResponseRetryCodes.includes(statusCode) &&
!this._noRetryOnCodes.includes(statusCode)
) {
return true
} else {
return false
}
}
}

const lowercaseKeys = (obj: {[index: string]: any}): any =>
Expand Down
2 changes: 2 additions & 0 deletions packages/http-client/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ export interface RequestOptions {
// Allows retries only on Read operations (since writes may not be idempotent)
allowRetries?: boolean
maxRetries?: number
retryCodes?: number[]
noRetryCodes?: number[]
}

export interface TypedResponse<T> {
Expand Down