chore(fetcher): retry 5xx errors up to 3 times

This commit is contained in:
WebStreamr 2025-08-13 10:49:36 +00:00
parent 17da20f6d0
commit a12ae7aaa5
No known key found for this signature in database
2 changed files with 9 additions and 3 deletions

View file

@ -208,9 +208,9 @@ describe('fetch', () => {
}
});
test('passes through other error as HttpError', async () => {
test('passes through other error as HttpError after retrying 3 times', async () => {
const mockPool = mockAgent.get('https://some-error-url.test');
mockPool.intercept({ path: '/' }).reply(500, undefined, { headers: { 'x-foo': 'bar' } });
mockPool.intercept({ path: '/' }).reply(500, undefined, { headers: { 'x-foo': 'bar' } }).times(4);
try {
await fetcher.text(ctx, new URL('https://some-error-url.test/'));

View file

@ -248,7 +248,7 @@ export class Fetcher {
return this.handleHttpCacheItem(ctx, httpCacheItem, url, init);
};
protected async fetchWithTimeout(ctx: Context, url: URL, init?: CustomRequestInit): Promise<Response> {
protected async fetchWithTimeout(ctx: Context, url: URL, init?: CustomRequestInit, tryCount = 0): Promise<Response> {
this.logger.info(`Fetch ${init?.method ?? 'GET'} ${url}`, ctx);
if (this.rateLimitedCache.has(url.host)) {
@ -279,6 +279,12 @@ export class Fetcher {
await this.decreaseTimeoutsCount(url);
if (response.status >= 500 && tryCount < 3) {
await new Promise(sleep => setTimeout(sleep, 333));
return await this.fetchWithTimeout(ctx, url, init, ++tryCount);
}
return response;
};