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

chore(retry): extend code set and add exponential backoff #92

Merged
merged 1 commit into from
Mar 18, 2024
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"dependencies": {
"@ai-zen/node-fetch-event-source": "^2.1.2",
"fetch-retry": "^5.0.6",
"http-status-codes": "^2.3.0",
"openapi-fetch": "^0.8.2",
"p-queue": "^8.0.1",
"yaml": "^2.3.3"
Expand Down
17 changes: 16 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fetchRetry from 'fetch-retry';
import { StatusCodes } from 'http-status-codes';

import { InvalidInputError } from './errors.js';
import { version } from './buildInfo.js';
Expand Down Expand Up @@ -53,8 +54,22 @@ export class Client {
const _client = createApiClient({
baseUrl: endpoint,
headers,
fetch: fetchRetry(fetch, { retryOn: [429] }), // Retry on network errors and when rate limits or concurrency limits (due to external factors) are hit
fetch: fetchRetry(fetch, {
retryOn: [
StatusCodes.TOO_MANY_REQUESTS, // Retry also when concurrency limits (due to external factors) are hit
StatusCodes.BAD_GATEWAY,
StatusCodes.SERVICE_UNAVAILABLE,
StatusCodes.CONFLICT,
StatusCodes.GATEWAY_TIMEOUT,
StatusCodes.REQUEST_TIMEOUT,
StatusCodes.INTERNAL_SERVER_ERROR,
],
retryDelay: function (attempt) {
return Math.pow(2, attempt) * 1000;
},
}),
});

const _streamingClient = createStreamingApiClient({
baseUrl: endpoint,
headers,
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ describe('client', () => {
describe('limits', () => {
test('should handle rate limits', async () => {
const promise = Promise.all(
range(0, 100).map(() => client.tune.types({})),
range(0, 50).map(() => client.tune.types({})),
);
await expect(promise).toResolve();
});
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ __metadata:
eslint-import-resolver-typescript: ^3.6.1
eslint-plugin-import: ^2.29.0
fetch-retry: ^5.0.6
http-status-codes: ^2.3.0
husky: ^8.0.3
jest-extended: ^4.0.2
lint-staged: ^15.0.2
Expand Down Expand Up @@ -3620,6 +3621,13 @@ __metadata:
languageName: node
linkType: hard

"http-status-codes@npm:^2.3.0":
version: 2.3.0
resolution: "http-status-codes@npm:2.3.0"
checksum: dae3b99e0155441b6df28e8265ff27c56a45f82c6092f736414233e9ccf063d5ea93c1e1279e8b499c4642e2538b37995c76b1640ed3f615d0e2883d3a1dcfd5
languageName: node
linkType: hard

"https-proxy-agent@npm:^5.0.0":
version: 5.0.1
resolution: "https-proxy-agent@npm:5.0.1"
Expand Down