agents
Version:
A home for your AI agents
82 lines (81 loc) • 3.84 kB
JavaScript
//#region src/retries.ts
/**
* Validate retry options eagerly so invalid config fails at enqueue/schedule time
* rather than at execution time. Checks individual field ranges, enforces integer
* maxAttempts, and validates cross-field constraints after resolving against
* defaults when provided.
*/
function validateRetryOptions(options, defaults) {
if (options.maxAttempts !== void 0) {
if (!Number.isFinite(options.maxAttempts) || options.maxAttempts < 1) throw new Error("retry.maxAttempts must be >= 1");
if (!Number.isInteger(options.maxAttempts)) throw new Error("retry.maxAttempts must be an integer");
}
if (options.baseDelayMs !== void 0) {
if (!Number.isFinite(options.baseDelayMs) || options.baseDelayMs <= 0) throw new Error("retry.baseDelayMs must be > 0");
}
if (options.maxDelayMs !== void 0) {
if (!Number.isFinite(options.maxDelayMs) || options.maxDelayMs <= 0) throw new Error("retry.maxDelayMs must be > 0");
}
const resolvedBase = options.baseDelayMs ?? defaults?.baseDelayMs;
const resolvedMax = options.maxDelayMs ?? defaults?.maxDelayMs;
if (resolvedBase !== void 0 && resolvedMax !== void 0 && resolvedBase > resolvedMax) throw new Error("retry.baseDelayMs must be <= retry.maxDelayMs");
}
/**
* Returns the number of milliseconds to wait before retrying a request.
* Uses the "Full Jitter" approach from
* https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
*
* @param attempt The current attempt number (1-indexed).
* @param baseDelayMs Base delay multiplier in ms.
* @param maxDelayMs Maximum delay cap in ms.
* @returns Milliseconds to wait before retrying.
*/
function jitterBackoff(attempt, baseDelayMs, maxDelayMs) {
const upperBoundMs = Math.min(2 ** attempt * baseDelayMs, maxDelayMs);
return Math.floor(Math.random() * upperBoundMs);
}
/**
* Retry an async function up to `n` total attempts with jittered exponential backoff.
*
* @param n Total number of attempts (must be a finite integer >= 1).
* @param fn The async function to retry. Receives the current attempt number (1-indexed).
* @param options Retry configuration.
* @returns The result of `fn` on success.
* @throws The last error if all attempts fail or `shouldRetry` returns false.
*/
async function tryN(n, fn, options) {
if (!Number.isFinite(n) || n < 1) throw new Error("retry.maxAttempts must be >= 1");
n = Math.floor(n);
const rawBase = options?.baseDelayMs ?? 100;
const rawMax = options?.maxDelayMs ?? 3e3;
if (!Number.isFinite(rawBase) || rawBase <= 0) throw new Error("retry.baseDelayMs must be > 0");
if (!Number.isFinite(rawMax) || rawMax <= 0) throw new Error("retry.maxDelayMs must be > 0");
const baseDelayMs = Math.floor(rawBase);
const maxDelayMs = Math.floor(rawMax);
if (baseDelayMs > maxDelayMs) throw new Error("retry.baseDelayMs must be <= retry.maxDelayMs");
let attempt = 1;
while (true) try {
return await fn(attempt);
} catch (err) {
const nextAttempt = attempt + 1;
if (nextAttempt > n || options?.shouldRetry && !options.shouldRetry(err, nextAttempt)) throw err;
const delay = jitterBackoff(attempt, baseDelayMs, maxDelayMs);
await new Promise((resolve) => setTimeout(resolve, delay));
attempt = nextAttempt;
}
}
/**
* Returns true if the given error is retryable according to Durable Object error handling.
* See https://developers.cloudflare.com/durable-objects/best-practices/error-handling/
*
* An error is retryable if it has `retryable: true` but is NOT an overloaded error.
*/
function isErrorRetryable(err) {
if (typeof err !== "object" || err === null) return false;
const msg = String(err);
const typed = err;
return Boolean(typed.retryable) && !typed.overloaded && !msg.includes("Durable Object is overloaded");
}
//#endregion
export { isErrorRetryable, jitterBackoff, tryN, validateRetryOptions };
//# sourceMappingURL=retries.js.map