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

fix(utils): stop immediately when retry limited #576

Merged
merged 2 commits into from
Nov 29, 2023
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
49 changes: 34 additions & 15 deletions packages/utils/src/async/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,22 @@ export function timeout<T>(
typeof options === "number" ? options : options.milliseconds ?? 1000;
const message = typeof options === "number" ? undefined : options.message;

const timeoutPromise = delay(milliseconds).then(() =>
Promise.reject(
message instanceof Error ? message : createTimeoutError(message)
)
);
return new Promise((resolve, reject) => {
const timeoutTask = setTimeout(() => {
reject(message instanceof Error ? message : createTimeoutError(message));
}, milliseconds);

return race<T>([promise, timeoutPromise]);
promise.then(
(value) => {
clearTimeout(timeoutTask);
resolve(value);
},
(error) => {
clearTimeout(timeoutTask);
reject(error);
}
);
});
}

export interface RetryOptions {
Expand All @@ -73,20 +82,30 @@ export function retry<T>(
delay: delayMs = 0,
} = options;

let lastErr: unknown;
let times = 0;
let currentRetryTimes = 0;

const retryPromise = new Promise<T>((resolve, reject) => {
function retryRun() {
times++;
if (times > retries) {
reject(lastErr);
function handleError(err: unknown) {
if (currentRetryTimes > retries) {
reject(err);
return;
}
Promise.resolve(run()).then(resolve, (e) => {
lastErr = e;

currentRetryTimes++;

if (delayMs) {
delay(delayMs).then(retryRun);
});
} else {
retryRun();
}
}

function retryRun() {
try {
Promise.resolve(run()).then(resolve, handleError);
} catch (err) {
handleError(err);
}
}

retryRun();
Expand Down
27 changes: 23 additions & 4 deletions packages/utils/tests/async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ test("async#timeout", async (t) => {

test("async#retry", async (t) => {
function passOn(times: number): () => Promise<void> {
let count = 0;
let runTimes = 0;
return () =>
new Promise<void>((resolve, reject) => {
count++;
if (count >= times) resolve();
else reject(new Error("Failed"));
if (runTimes > times) {
return resolve();
}
runTimes++;
reject(new Error("Failed"));
});
}

Expand Down Expand Up @@ -71,3 +73,20 @@ test("async#retry with timeout", async (t) => {
t.true(called);
t.true(failed);
});

test("async#retry with delay", async (t) => {
const before = Date.now();

await t.throwsAsync(() =>
retry(
() => {
throw new Error("delay");
},
{ retries: 5, delay: 50, timeout: 1000 }
)
);

const after = Date.now();

t.true(after - before >= 50 * 5);
});
Loading