akshitkrnagpal / akshitkrnagpal/keyv-dataloader
Implement Redis connection retry logic for better resilience
- Lenguaje dominante
- TypeScript
- Estrellas
- 1
- Forks
- 0
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
When using Redis as the cache store, the library currently doesn't implement any connection retry logic. This can cause issues in production environments where Redis connections might temporarily fail.
### Issues
1. Temporary Redis connection issues can cause the entire dataloader to fail
2. No automatic reconnection strategy is implemented
3. Test coverage for connection failure scenarios is lacking
### Proposed Solution
1. Add a connection retry option:
```typescript
export interface KeyvDataLoaderOptions {
// ... existing options
/**
* Redis connection retry configuration
*/
connectionRetry?: {
/**
* Maximum number of retries
*/
maxRetries?: number;
/**
* Delay between retries in milliseconds
*/
retryDelay?: number;
/**
* Whether to use exponential backoff for retries
*/
useExponentialBackoff?: boolean;
};
}
```
2. Implement a connection retry wrapper for Redis operations:
```typescript
private async withRetry(operation: () => Promise): Promise {
const { maxRetries = 3, retryDelay = 1000, useExponentialBackoff = true } =
this.options.connectionRetry || {};
let lastError: Error;
for (let attempt = 0; attempt < maxRetries; attempt++) {
try {
return await operation();
} catch (error) {
lastError = error as Error;
// Only retry if it's a connection error
if (!(error instanceof Error) || !error.message.includes('connection')) {
throw error;
}
// Calculate delay with exponential backoff if enabled
const delay = useExponentialBackoff
? retryDelay * Math.pow(2, attempt)
: retryDelay;
await new Promise(resolve => setTimeout(resolve, delay));
}
}
throw lastError;
}
```
3. Add test cases to verify the retry behavior:
```typescript
test('should retry Redis connection on temporary failure', async () => {
// Mock implementation with connection simulation
});
```
This enhancement would make the library more resilient in production environments with unreliable Redis connections.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Evaluación
Este issue todavía no se ha evaluado.