akshitkrnagpal / akshitkrnagpal/keyv-dataloader

Implement Redis connection retry logic for better resilience

Ouverte
#6 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
enhancement
Langage dominant
TypeScript
Étoiles
1
Forks
0
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

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.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.