Skip to content

Commit ad96f2c

Browse files
authored
fix: reconnected should also be fired before retrying the command (#482)
1 parent d9210ea commit ad96f2c

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

Diff for: connection.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,18 @@ export class RedisConnection
360360
return command.reject(error);
361361
}
362362

363+
let backoff = 0;
363364
for (let i = 0; i < this.maxRetryCount; i++) {
364365
// Try to reconnect to the server and retry the command
365366
this.#close(true);
366367
try {
368+
this.#dispatchEvent("reconnecting", { delay: backoff });
367369
await this.connect();
368370
const reply = await command.execute();
369371
return command.resolve(reply);
370-
} catch { // TODO: use `AggregateError`?
371-
const backoff = this.backoff(i);
372+
} catch (error) {
373+
this.#dispatchEvent("error", { error }); // TODO: use `AggregateError`?
374+
backoff = this.backoff(i);
372375
await delay(backoff);
373376
}
374377
}

Diff for: tests/reconnect_test.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assertEquals } from "../deps/std/assert.ts";
1+
import { assertEquals, assertInstanceOf } from "../deps/std/assert.ts";
22
import { beforeAll, describe, it } from "../deps/std/testing.ts";
33
import { newClient, nextPort, startRedis, stopRedis } from "./test_util.ts";
44

@@ -13,10 +13,16 @@ describe("reconnect", () => {
1313
const client = await newClient({ hostname: "127.0.0.1", port });
1414
assertEquals(await client.ping(), "PONG");
1515
await stopRedis(server);
16+
let reconnectingFired = 0;
17+
client.addEventListener("reconnecting", (e) => {
18+
reconnectingFired++;
19+
assertInstanceOf(e, CustomEvent);
20+
});
1621
server = await startRedis({ port });
1722
assertEquals(await client.ping(), "PONG");
1823
client.close();
1924
await stopRedis(server);
25+
assertEquals(reconnectingFired, 1);
2026
});
2127

2228
it("auto reconnect, with db spec", async () => {

0 commit comments

Comments
 (0)