Skip to content

Commit 4733cd1

Browse files
authored
proper reconnect implementation (#98)
1 parent e5b50e4 commit 4733cd1

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

Diff for: src/redis-adapter.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,18 @@ const _noReconnectStrategy = () => new VError({ name: VERROR_CLUSTER_NAME }, "di
157157
const _createClientBase = (clientName, options = {}) => {
158158
const { doReconnect = true } = options;
159159
try {
160-
const [isCluster, redisClientOptions] = _getRedisOptionsTuple();
160+
const [isCluster, sharedRedisClientOptions] = _getRedisOptionsTuple();
161161

162-
if (!doReconnect) {
163-
redisClientOptions.socket.reconnectStrategy = _noReconnectStrategy;
164-
}
162+
// NOTE: we need to take care not to pollute sharedRedisClientOptions for subsequent calls of other clients
163+
const redisClientOptions = doReconnect
164+
? sharedRedisClientOptions
165+
: {
166+
...sharedRedisClientOptions,
167+
socket: {
168+
...sharedRedisClientOptions.socket,
169+
reconnectStrategy: _noReconnectStrategy,
170+
},
171+
};
165172

166173
return isCluster
167174
? redis.createCluster({
@@ -632,6 +639,7 @@ const removeAllMessageHandlers = (channel) => __messageHandlers.removeAllHandler
632639

633640
module.exports = {
634641
REDIS_INTEGRATION_MODE: INTEGRATION_MODE,
642+
REDIS_CLIENT_NAME: CLIENT_NAME,
635643
setCustomOptions,
636644
getIntegrationMode,
637645
getMainClient,

Diff for: test/redis-adapter.test.js

+36
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ jest.mock("@redis/client", () => ({
3636

3737
const redisAdapter = require("../src/redis-adapter");
3838

39+
const { REDIS_CLIENT_NAME: CLIENT_NAME } = redisAdapter;
40+
3941
const channel = "channel";
4042
const channelTwo = "channelTwo";
4143
const message = "message";
@@ -82,6 +84,40 @@ describe("redis-adapter test", () => {
8284
expect(loggerSpy.error).not.toHaveBeenCalled();
8385
});
8486

87+
test("_createClientAndConnect silent and then main does not leak options", async () => {
88+
await redisAdapter._._createClientAndConnect(CLIENT_NAME.SILENT, {
89+
doLogEvents: false,
90+
doReconnect: false,
91+
});
92+
93+
expect(redis.createClient).toHaveBeenCalledTimes(1);
94+
expect(redis.createClient.mock.calls[0]).toMatchInlineSnapshot(`
95+
[
96+
{
97+
"pingInterval": 240000,
98+
"socket": {
99+
"reconnectStrategy": [Function],
100+
},
101+
},
102+
]
103+
`);
104+
105+
redis.createClient.mockClear();
106+
await redisAdapter._._createClientAndConnect(CLIENT_NAME.MAIN);
107+
108+
expect(redis.createClient).toHaveBeenCalledTimes(1);
109+
expect(redis.createClient.mock.calls[0]).toMatchInlineSnapshot(`
110+
[
111+
{
112+
"pingInterval": 240000,
113+
"socket": {},
114+
},
115+
]
116+
`);
117+
118+
expect(loggerSpy.error).not.toHaveBeenCalled();
119+
});
120+
85121
test.each(Object.entries(require("./__mocks__/mock-redis-credentials.json")))(
86122
"_createClientBase on CF | %s",
87123
async (_, credentials) => {

0 commit comments

Comments
 (0)