Skip to content

Commit 7d6d7cb

Browse files
committed
fix: fix pr comments
1 parent 991af29 commit 7d6d7cb

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

lib/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Socket, Connection } from "./socket";
44
export { Integration } from "./interfaces/integration";
55
export { getRedisClient, FoundQueue, RedisConnection } from "./queue-factory";
66
export { WebSocketClient } from "./ws-autoreconnect";
7-
export { Connection, ConnectionOptions, RedisConnection as RedisClient } from "./socket";
7+
export { Connection, ConnectionOptions } from "./socket";
88
export { respond } from "./responders/respond";
99
export { BullMQResponders } from "./responders/bullmq-responders";
1010

lib/queue-factory.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,31 @@ export async function getRedisInfo(
166166
return info;
167167
}
168168

169+
/**
170+
* Gets or creates a Redis client for queue operations.
171+
*
172+
* @param redisOpts - Redis connection options. Required if existingClient is not provided.
173+
* @param type - The type of queue ("bull" or "bullmq"), used for client caching key.
174+
* @param clusterNodes - Optional list of cluster node URIs for Redis Cluster.
175+
* @param existingClient - Optional pre-configured Redis/Cluster instance.
176+
* @returns A Redis or Cluster client.
177+
*
178+
* @remarks
179+
* When `existingClient` is provided, it is returned directly without caching.
180+
* This allows the caller to manage the client lifecycle independently.
181+
*
182+
* When `redisOpts` are provided (without existingClient), the created client
183+
* is cached internally using a checksum of the options. Subsequent calls with
184+
* the same options will reuse the cached client.
185+
*/
169186
export function getRedisClient(
170187
redisOpts: RedisOptions | undefined,
171188
type: "bull" | "bullmq",
172189
clusterNodes?: string[],
173190
existingClient?: RedisConnection
174191
) {
175-
// If we have an existing client, use it directly
192+
// If an existing client is provided, return it directly.
193+
// We don't cache it since the caller owns its lifecycle.
176194
if (existingClient) {
177195
return existingClient;
178196
}
@@ -181,7 +199,7 @@ export function getRedisClient(
181199
throw new Error("Redis options are required when no client is provided");
182200
}
183201

184-
// Compute checksum for redisOpts
202+
// Compute checksum for redisOpts to use as cache key
185203
const checksumJson = JSON.stringify(redisOpts);
186204
const checksum = require("crypto")
187205
.createHash("md5")

lib/queues-cache.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import * as Bull from "bull";
22
import { Queue } from "bullmq";
3-
import { Redis, Cluster, RedisOptions } from "ioredis";
3+
import { RedisOptions } from "ioredis";
44
import { keyBy } from "lodash";
5-
import { FoundQueue, createQueue, getConnectionQueues } from "./queue-factory";
5+
import {
6+
FoundQueue,
7+
RedisConnection,
8+
createQueue,
9+
getConnectionQueues,
10+
} from "./queue-factory";
611
import { Responders } from "./interfaces/responders";
712
import { Integration } from "./interfaces/integration";
813

9-
export type RedisConnection = Redis | Cluster;
10-
1114
let queuesCache: {
1215
[index: string]: { queue: Bull.Queue | Queue; responders: Responders };
1316
} = null;

lib/socket.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@ import { getCache, updateQueuesCache, queueKey } from "./queues-cache";
44
import { WebSocketClient } from "./ws-autoreconnect";
55
import {
66
FoundQueue,
7+
RedisConnection,
78
execRedisCommand,
89
getRedisInfo,
910
ping,
1011
} from "./queue-factory";
1112
import { getQueueType, redisOptsFromUrl } from "./utils";
1213
import { Integration } from "./interfaces/integration";
1314

14-
export type RedisConnection = Redis | Cluster;
15-
1615
const { version } = require(`${__dirname}/../package.json`);
1716

1817
const chalk = require("chalk");

0 commit comments

Comments
 (0)