akshitkrnagpal / akshitkrnagpal/keyv-dataloader

Implement Redis connection retry logic for better resilience

未关闭
#6 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
enhancement
主要语言
TypeScript
星标
1
派生
0
PR 合并指标
30 天内没有已合并 PR

描述

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.

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。