mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-22 15:11:13 -05:00
feat: Implement circuit breaker and retry logic
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
*/
|
||||
|
||||
import { logger } from './logger';
|
||||
import { supabaseCircuitBreaker } from './circuitBreaker';
|
||||
|
||||
export interface RetryOptions {
|
||||
/** Maximum number of attempts (default: 3) */
|
||||
@@ -135,8 +136,10 @@ export async function withRetry<T>(
|
||||
|
||||
for (let attempt = 0; attempt < config.maxAttempts; attempt++) {
|
||||
try {
|
||||
// Execute the function
|
||||
const result = await fn();
|
||||
// Execute the function through circuit breaker
|
||||
const result = await supabaseCircuitBreaker.execute(async () => {
|
||||
return await fn();
|
||||
});
|
||||
|
||||
// Log successful retry if not first attempt
|
||||
if (attempt > 0) {
|
||||
@@ -150,6 +153,15 @@ export async function withRetry<T>(
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
|
||||
// Check if circuit breaker blocked the request
|
||||
if (error instanceof Error && error.message.includes('Circuit breaker is OPEN')) {
|
||||
logger.error('Circuit breaker prevented retry', {
|
||||
attempt: attempt + 1,
|
||||
circuitState: supabaseCircuitBreaker.getState()
|
||||
});
|
||||
throw error; // Don't retry if circuit is open
|
||||
}
|
||||
|
||||
// Check if we should retry
|
||||
const isLastAttempt = attempt === config.maxAttempts - 1;
|
||||
const shouldRetry = config.shouldRetry(error);
|
||||
|
||||
Reference in New Issue
Block a user