Skip to content

Commit

Permalink
fixup: limit max concurrent requests on providers
Browse files Browse the repository at this point in the history
  • Loading branch information
mroz22 committed Sep 28, 2023
1 parent 635f040 commit f3ef29b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions suite-common/metadata-types/src/metadataTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,32 @@ export abstract class AbstractMetadataProvider {
} as const;
}

private apiRequestsNumber = 0;
scheduleApiRequest<T extends () => ReturnType<R>, R extends (...args: any) => Result<any>>(
fn: T,
options: { retries: number; delay: number } = { retries: 3, delay: 1000 },
options: { retries: number; delay: number; maxConcurrent: number } = {
retries: 3,
delay: 1000,
maxConcurrent: 10,
},
) {
let retried = 0;
return new Promise<Awaited<ReturnType<R>>>(resolve => {
const { retries, delay } = options;
const { retries, delay, maxConcurrent } = options;
const run = async () => {
const res = await fn();
if (this.apiRequestsNumber > maxConcurrent) {
setTimeout(run, delay);
return;
}

this.apiRequestsNumber++;

const res = await fn().finally(() => {
this.apiRequestsNumber--;
});

if (res.success) {
// @ts-expect-error adding finally above causes ts to have problem here. I don't know why
return resolve(res);
}

Expand All @@ -169,6 +184,7 @@ export abstract class AbstractMetadataProvider {
setTimeout(run, delay);
} else {
// reached retries limit, return error
// @ts-expect-error adding finally above causes ts to have problem here. I don't know why
resolve(res);
}
};
Expand Down

0 comments on commit f3ef29b

Please sign in to comment.