From 8ac4717023c822479b7d647aae920f6e3748d9bb Mon Sep 17 00:00:00 2001 From: Mark Sujew Date: Mon, 12 Aug 2024 21:15:28 +0200 Subject: [PATCH] Re-introduce loop --- .../ovsx-client/src/ovsx-http-client.ts | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/dev-packages/ovsx-client/src/ovsx-http-client.ts b/dev-packages/ovsx-client/src/ovsx-http-client.ts index 73ea8cd4d8640..087c550d16d7a 100644 --- a/dev-packages/ovsx-client/src/ovsx-http-client.ts +++ b/dev-packages/ovsx-client/src/ovsx-http-client.ts @@ -47,15 +47,22 @@ export class OVSXHttpClient implements OVSXClient { } protected async requestJson(url: string): Promise { - await this.rateLimiter.removeTokens(1); - const context = await this.requestService.request({ - url, - headers: { 'Accept': 'application/json' } - }); - if (context.res.statusCode === 429) { - console.warn('OVSX rate limit exceeded. Consider reducing the rate limit.'); + const attempts = 5; + for (let i = 0; i < attempts; i++) { + await this.rateLimiter.removeTokens(i + 1); + const context = await this.requestService.request({ + url, + headers: { 'Accept': 'application/json' } + }); + if (context.res.statusCode === 429) { + console.warn('OVSX rate limit exceeded. Will perform request with higher timeout. Consider reducing the rate limit.'); + if (i < attempts - 1) { + continue; + } + } + return RequestContext.asJson(context); } - return RequestContext.asJson(context); + throw new Error('Request timed out due to too many requests'); } protected buildUrl(url: string, query?: object): string {