Skip to content

Commit a365581

Browse files
committed
update retry
1 parent 82e0962 commit a365581

File tree

1 file changed

+49
-39
lines changed
  • src/content/docs/d1/tutorials/using-read-replication-for-e-com

1 file changed

+49
-39
lines changed

src/content/docs/d1/tutorials/using-read-replication-for-e-com/index.mdx

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -569,55 +569,65 @@ To make the application more resilient, you can add retry logic to the API route
569569

570570
```ts
571571
export interface RetryConfig {
572-
maxRetries?: number;
573-
initialDelay?: number;
574-
maxDelay?: number;
575-
backoffFactor?: number;
572+
maxRetries?: number;
573+
initialDelay?: number;
574+
maxDelay?: number;
575+
backoffFactor?: number;
576576
}
577577

578+
const shouldRetry = (error: unknown): boolean => {
579+
const errMsg = error instanceof Error ? error.message : String(error);
580+
return (
581+
errMsg.includes("Network connection lost") ||
582+
errMsg.includes("storage caused object to be reset") ||
583+
errMsg.includes("reset because its code was updated")
584+
);
585+
};
586+
578587
export const defaultRetryConfig: RetryConfig = {
579-
maxRetries: 3,
580-
initialDelay: 100,
581-
maxDelay: 1000,
582-
backoffFactor: 2,
588+
maxRetries: 3,
589+
initialDelay: 100,
590+
maxDelay: 1000,
591+
backoffFactor: 2,
583592
};
584593

585594
export async function withRetry<T>(
586-
operation: () => Promise<T>,
587-
config: RetryConfig = defaultRetryConfig,
595+
operation: () => Promise<T>,
596+
config: RetryConfig = defaultRetryConfig
588597
): Promise<T> {
589-
const maxRetries = config.maxRetries ?? defaultRetryConfig.maxRetries!;
590-
const initialDelay = config.initialDelay ?? defaultRetryConfig.initialDelay!;
591-
const maxDelay = config.maxDelay ?? defaultRetryConfig.maxDelay!;
592-
const backoffFactor =
593-
config.backoffFactor ?? defaultRetryConfig.backoffFactor!;
594-
595-
let lastError: Error | unknown;
596-
let delay = initialDelay;
597-
598-
for (let attempt = 0; attempt <= maxRetries; attempt++) {
599-
try {
600-
return await operation();
601-
} catch (error) {
602-
lastError = error;
603-
604-
if (attempt === maxRetries) {
605-
break;
606-
}
607-
608-
// Wait for the calculated delay
609-
await new Promise((resolve) => setTimeout(resolve, delay));
610-
611-
// Calculate next delay with exponential backoff
612-
delay = Math.min(delay * backoffFactor, maxDelay);
613-
}
614-
}
615-
616-
throw lastError;
598+
const maxRetries = config.maxRetries ?? defaultRetryConfig.maxRetries!;
599+
const initialDelay = config.initialDelay ?? defaultRetryConfig.initialDelay!;
600+
const maxDelay = config.maxDelay ?? defaultRetryConfig.maxDelay!;
601+
const backoffFactor =
602+
config.backoffFactor ?? defaultRetryConfig.backoffFactor!;
603+
604+
let lastError: Error | unknown;
605+
let delay = initialDelay;
606+
607+
for (let attempt = 0; attempt <= maxRetries; attempt++) {
608+
try {
609+
const result = await operation();
610+
return result;
611+
} catch (error) {
612+
lastError = error;
613+
614+
if (!shouldRetry(error) || attempt === maxRetries) {
615+
throw error;
616+
}
617+
618+
// Wait for the calculated delay
619+
await new Promise((resolve) => setTimeout(resolve, delay));
620+
621+
// Calculate next delay with exponential backoff
622+
delay = Math.min(delay * backoffFactor, maxDelay);
623+
}
624+
}
625+
626+
throw lastError;
617627
}
618628
```
619629

620-
The `withRetry` function is a utility function that retries a given operation with exponential backoff. It takes a configuration object as an argument, which allows you to customize the number of retries, initial delay, maximum delay, and backoff factor.
630+
The `withRetry` function is a utility function that retries a given operation with exponential backoff. It takes a configuration object as an argument, which allows you to customize the number of retries, initial delay, maximum delay, and backoff factor. It will only retry the operation if the error is due to a network connection loss, storage reset, or code update.
621631

622632
Next, update the `src/index.ts` file to import the `withRetry` function and use it in the API routes.
623633

0 commit comments

Comments
 (0)