-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rest-client): retry requests on network errors (#449)
* chore(rest-client): rename `retryIfThrottled` to `retryIfFailed` * feat(rest-client): retry requests on network errors
- Loading branch information
Showing
6 changed files
with
174 additions
and
121 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
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,114 @@ | ||
import { expect, jest } from '@jest/globals' | ||
import { Headers, Response } from '../lib/fetch/fetch.node' | ||
import { RestClientError } from './RestClientError' | ||
import { retryIfFailed } from './retryIfFailed' | ||
|
||
const THROTTLED_RESPONSE = new Response( | ||
JSON.stringify({ | ||
detail: 'Request was throttled. Expected available in 1 second.' | ||
}), | ||
{ | ||
status: 429, | ||
statusText: 'Too Many Requests', | ||
headers: new Headers({ | ||
'retry-after': '1' | ||
}) | ||
} | ||
) | ||
|
||
const createRunner = ({ | ||
error, | ||
attempts, | ||
response = new Response() | ||
}: { | ||
attempts: number | ||
error?: Error | ||
response?: Response | ||
}) => { | ||
let runs = 0 | ||
const mock = jest.fn(async () => { | ||
++runs | ||
|
||
if (runs <= attempts) { | ||
if (error) { | ||
throw error | ||
} | ||
return THROTTLED_RESPONSE | ||
} | ||
|
||
return response | ||
}) | ||
|
||
return mock | ||
} | ||
|
||
describe('retryIfFailed', () => { | ||
it("should be resolved with task's resolvee if was nor throttled nor failed", async () => { | ||
const response = new Response() | ||
const mock = createRunner({ attempts: 0, response }) | ||
|
||
await expect( | ||
retryIfFailed(mock, { | ||
retryThrottledRequestMaxTimes: 5, | ||
retryNetworkErrorMaxTimes: 5 | ||
}) | ||
).resolves.toEqual(response) | ||
expect(mock).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
describe('throttling', () => { | ||
it('should be rejected with RestClientError if was throttled and limit was left', async () => { | ||
const mock = createRunner({ attempts: 10 }) | ||
|
||
await expect( | ||
retryIfFailed(mock, { | ||
retryThrottledRequestMaxTimes: 5, | ||
retryNetworkErrorMaxTimes: 0 | ||
}) | ||
).rejects.toThrowError(RestClientError) | ||
expect(mock).toHaveBeenCalledTimes(6) | ||
}) | ||
|
||
it("should be resolved with task's resolvee if was throttled and limit was not left", async () => { | ||
const response = new Response() | ||
const mock = createRunner({ attempts: 3, response }) | ||
|
||
await expect( | ||
retryIfFailed(mock, { | ||
retryThrottledRequestMaxTimes: 10, | ||
retryNetworkErrorMaxTimes: 0 | ||
}) | ||
).resolves.toEqual(response) | ||
expect(mock).toHaveBeenCalledTimes(4) | ||
}) | ||
}) | ||
|
||
describe('network errors', () => { | ||
it("should be rejected with tasks's error if limit was left", async () => { | ||
const error = new Error() | ||
const mock = createRunner({ attempts: 5, error }) | ||
|
||
await expect( | ||
retryIfFailed(mock, { | ||
retryThrottledRequestMaxTimes: 0, | ||
retryNetworkErrorMaxTimes: 2 | ||
}) | ||
).rejects.toThrowError(error) | ||
expect(mock).toHaveBeenCalledTimes(3) | ||
}) | ||
|
||
it("should be resolved with tasks's resolvee if limit was not left", async () => { | ||
const error = new Error() | ||
const response = new Response() | ||
const mock = createRunner({ attempts: 2, error, response }) | ||
|
||
await expect( | ||
retryIfFailed(mock, { | ||
retryThrottledRequestMaxTimes: 0, | ||
retryNetworkErrorMaxTimes: 5 | ||
}) | ||
).resolves.toEqual(response) | ||
expect(mock).toHaveBeenCalledTimes(3) | ||
}) | ||
}) | ||
}) |
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,48 @@ | ||
import { retrier } from '@uploadcare/api-client-utils' | ||
import { RestClientError } from './RestClientError' | ||
|
||
const THROTTLED_STATUS = 429 | ||
const DEFAULT_RETRY_AFTER_TIMEOUT = 15000 | ||
const DEFAULT_NETWORK_ERROR_TIMEOUT = 1000 | ||
|
||
function getTimeoutFromThrottledRequest(response: Response): number { | ||
const { headers } = response | ||
if (!headers || !headers.get('retry-after')) { | ||
return DEFAULT_RETRY_AFTER_TIMEOUT | ||
} | ||
const seconds = parseInt(headers.get('retry-after') as string, 10) | ||
if (!Number.isFinite(seconds)) { | ||
return DEFAULT_RETRY_AFTER_TIMEOUT | ||
} | ||
return seconds * 1000 | ||
} | ||
|
||
export function retryIfFailed( | ||
fn: () => Promise<Response>, | ||
options: { | ||
retryThrottledRequestMaxTimes: number | ||
retryNetworkErrorMaxTimes: number | ||
} | ||
): Promise<Response> { | ||
return retrier(({ attempt, retry }) => | ||
fn() | ||
.then(async (response) => { | ||
if (response.status !== THROTTLED_STATUS) { | ||
return response | ||
} | ||
if (attempt < options.retryThrottledRequestMaxTimes) { | ||
return retry(getTimeoutFromThrottledRequest(response)) | ||
} | ||
const json = await response.json() | ||
const { detail } = json | ||
throw new RestClientError(detail, { response }) | ||
}) | ||
.catch((error) => { | ||
if (attempt < options.retryNetworkErrorMaxTimes) { | ||
return retry((attempt + 1) * DEFAULT_NETWORK_ERROR_TIMEOUT) | ||
} | ||
|
||
throw error | ||
}) | ||
) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.