refactor: share backoff helpers
This commit is contained in:
26
src/infra/backoff.ts
Normal file
26
src/infra/backoff.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { setTimeout as delay } from "node:timers/promises";
|
||||
|
||||
export type BackoffPolicy = {
|
||||
initialMs: number;
|
||||
maxMs: number;
|
||||
factor: number;
|
||||
jitter: number;
|
||||
};
|
||||
|
||||
export function computeBackoff(policy: BackoffPolicy, attempt: number) {
|
||||
const base = policy.initialMs * policy.factor ** Math.max(attempt - 1, 0);
|
||||
const jitter = base * policy.jitter * Math.random();
|
||||
return Math.min(policy.maxMs, Math.round(base + jitter));
|
||||
}
|
||||
|
||||
export async function sleepWithAbort(ms: number, abortSignal?: AbortSignal) {
|
||||
if (ms <= 0) return;
|
||||
try {
|
||||
await delay(ms, undefined, { signal: abortSignal });
|
||||
} catch (err) {
|
||||
if (abortSignal?.aborted) {
|
||||
throw new Error("aborted");
|
||||
}
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user