akshitkrnagpal / akshitkrnagpal/keyv-dataloader

Implement Redis connection retry logic for better resilience

Aperta
#6 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub
enhancement
Lingua principale
TypeScript
Stelle
1
Fork
0
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

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.

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.