Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: bound redis command timeout (#15091)

YuTengjing422ccc9f585b
diff --git a/src/libs/redis/redis.test.ts b/src/libs/redis/redis.test.ts
index c3877981927..6bb3f21d050 100644
--- a/src/libs/redis/redis.test.ts
+++ b/src/libs/redis/redis.test.ts
@@ -23,6 +23,8 @@ const buildRedisConfig = (): RedisConfig | null => {
const loadRedisProvider = async () => (await import('./redis')).IoRedisRedisProvider;
const createMockedProvider = async () => {
+ const instances: Array<{ options: Record<PropertyKey, unknown>; url: string }> = [];
+
const createPipelineMock = () => {
const pipeMocks = {
incr: vi.fn(),
@@ -77,8 +79,10 @@ const createMockedProvider = async () => {
class FakeRedis {
constructor(
public url: string,
- public options: any,
- ) {}
+ public options: Record<PropertyKey, unknown>,
+ ) {
+ instances.push({ options, url });
+ }
connect = mocks.connect;
ping = mocks.ping;
quit = mocks.quit;
@@ -114,7 +118,7 @@ const createMockedProvider = async () => {
await provider.initialize();
- return { mocks, provider };
+ return { instances, mocks, provider };
};
const shouldSkipIntegration = (error: unknown) =>
@@ -163,6 +167,22 @@ describe('integrated', (test) => {
});
describe('mocked', () => {
+ it('sets bounded ioredis connection and command timeouts', async () => {
+ const { instances, provider } = await createMockedProvider();
+
+ expect(instances).toHaveLength(1);
+ expect(instances[0]).toMatchObject({
+ options: {
+ commandTimeout: 10_000,
+ connectTimeout: 10_000,
+ maxRetriesPerRequest: 2,
+ },
+ url: 'redis://localhost:6379',
+ });
+
+ await provider.disconnect();
+ });
+
it('normalizes set options into ioredis arguments', async () => {
const { mocks, provider } = await createMockedProvider();
await provider.set('key', 'value', { ex: 10, nx: true, get: true });
diff --git a/src/libs/redis/redis.ts b/src/libs/redis/redis.ts
index c47fe9b0a9b..117e65efc50 100644
--- a/src/libs/redis/redis.ts
+++ b/src/libs/redis/redis.ts
@@ -15,6 +15,9 @@ import { buildIORedisSetArgs, normalizeMsetValues } from './utils';
const log = debug('lobe:redis');
+const REDIS_CONNECT_TIMEOUT_MS = 10_000;
+const REDIS_COMMAND_TIMEOUT_MS = 10_000;
+
export class IoRedisRedisProvider implements BaseRedisProvider {
private client: Redis | null = null;
@@ -24,6 +27,8 @@ export class IoRedisRedisProvider implements BaseRedisProvider {
const IORedis = await import('ioredis');
this.client = new IORedis.default(this.config.url, {
+ commandTimeout: REDIS_COMMAND_TIMEOUT_MS,
+ connectTimeout: REDIS_CONNECT_TIMEOUT_MS,
db: this.config.database,
keyPrefix: this.config.prefix ? `${this.config.prefix}:` : undefined,
lazyConnect: true,