feat: Implement circuit breaker and retry logic

This commit is contained in:
gpt-engineer-app[bot]
2025-11-05 13:27:22 +00:00
parent 5e0640252c
commit ec5181b9e6
7 changed files with 664 additions and 245 deletions

View File

@@ -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);