Skip to content

Commit

Permalink
Introduce minor delay
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Jan 30, 2025
1 parent 71b89bd commit df142fa
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions dev-packages/localization-manager/src/deepl-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export async function deepl(
const parameterCopy: DeeplParameters = { ...parameters, text: chunk };
const url = `https://${sub_domain}.deepl.com/v2/translate`;
const buffer = Buffer.from(toFormData(parameterCopy));
return postWithRetry(url, buffer);
return postWithRetry(url, buffer, 1);
}));
const mergedResponse: DeeplResponse = { translations: [] };
for (const response of responses) {
Expand All @@ -52,17 +52,17 @@ export async function deepl(
return mergedResponse;
}

async function postWithRetry(url: string, buffer: Buffer): Promise<DeeplResponse> {
async function postWithRetry(url: string, buffer: Buffer, attempt: number): Promise<DeeplResponse> {
try {
await rateLimiter.removeTokens(1);
await rateLimiter.removeTokens(attempt);
const response = await post(url, buffer, {
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': 'Theia-Localization-Manager'
});
return response;
} catch (e) {
if ('message' in e && typeof e.message === 'string' && e.message.includes('Too Many Requests')) {
return postWithRetry(url, buffer);
return postWithRetry(url, buffer, attempt + 1);
}
throw e;
}
Expand Down

0 comments on commit df142fa

Please sign in to comment.