MCPcopy Create free account
hub / github.com/boringstack-xyz/boringstack / retryWithBackoff

Function retryWithBackoff

apps/api/src/lib/email/email.utils.ts:178–205  ·  view source on GitHub ↗
(
  fn: () => Promise<T>,
  options: IRetryOptions = {}
)

Source from the content-addressed store, hash-verified

176 * the first attempt so the caller can react quickly.
177 */
178export const retryWithBackoff = async <T>(
179 fn: () => Promise<T>,
180 options: IRetryOptions = {}
181): Promise<T> => {
182 const config: IResolvedRetryOptions = { ...RETRY_DEFAULTS, ...options };
183 let lastError: unknown;
184
185 for (let attempt = 0; attempt <= config.maxRetries; attempt++) {
186 try {
187 return await fn();
188 } catch (error: unknown) {
189 lastError = error;
190
191 if (
192 attempt === config.maxRetries ||
193 !isRetryableError(error, config.retryableErrorTypes)
194 ) {
195 throw error;
196 }
197
198 const delayMs = config.retryDelayMs * Math.pow(2, attempt);
199
200 await delay(delayMs);
201 }
202 }
203
204 throw lastError;
205};
206
207/*
208 * ---------------------------------------------------------------------------

Callers 5

sendMethod · 0.90
sendMethod · 0.90
sendMethod · 0.90
sendMethod · 0.90
retry.test.tsFile · 0.90

Calls 2

isRetryableErrorFunction · 0.85
delayFunction · 0.85

Tested by

no test coverage detected