From 91d80db36eb7934b577620394b3dd2b4b2a08b8a Mon Sep 17 00:00:00 2001 From: uki00a Date: Tue, 10 Dec 2024 22:49:19 +0900 Subject: [PATCH 1/4] test: `reconnecting` event --- tests/reconnect_test.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tests/reconnect_test.ts b/tests/reconnect_test.ts index 8f6b907c..4a9f4278 100644 --- a/tests/reconnect_test.ts +++ b/tests/reconnect_test.ts @@ -1,4 +1,4 @@ -import { assertEquals } from "../deps/std/assert.ts"; +import { assertEquals, assertInstanceOf } from "../deps/std/assert.ts"; import { beforeAll, describe, it } from "../deps/std/testing.ts"; import { newClient, nextPort, startRedis, stopRedis } from "./test_util.ts"; @@ -14,9 +14,15 @@ describe("reconnect", () => { assertEquals(await client.ping(), "PONG"); await stopRedis(server); server = await startRedis({ port }); + let reconnectingFired = 0; + client.addEventListener("reconnecting", (e) => { + reconnectingFired++; + assertInstanceOf(e, CustomEvent); + }); assertEquals(await client.ping(), "PONG"); client.close(); await stopRedis(server); + assertEquals(reconnectingFired, 1); }); it("auto reconnect, with db spec", async () => { From 1101fec1280ee79331ffae4f70188435df79a79d Mon Sep 17 00:00:00 2001 From: uki00a Date: Tue, 10 Dec 2024 22:54:56 +0900 Subject: [PATCH 2/4] fix --- tests/reconnect_test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/reconnect_test.ts b/tests/reconnect_test.ts index 4a9f4278..80f0eab6 100644 --- a/tests/reconnect_test.ts +++ b/tests/reconnect_test.ts @@ -13,12 +13,12 @@ describe("reconnect", () => { const client = await newClient({ hostname: "127.0.0.1", port }); assertEquals(await client.ping(), "PONG"); await stopRedis(server); - server = await startRedis({ port }); let reconnectingFired = 0; client.addEventListener("reconnecting", (e) => { reconnectingFired++; assertInstanceOf(e, CustomEvent); }); + server = await startRedis({ port }); assertEquals(await client.ping(), "PONG"); client.close(); await stopRedis(server); From 7e0a5db63545083b584caa86b300f53448d99191 Mon Sep 17 00:00:00 2001 From: uki00a Date: Tue, 10 Dec 2024 23:12:27 +0900 Subject: [PATCH 3/4] fix --- connection.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/connection.ts b/connection.ts index 1754da81..8179ff96 100644 --- a/connection.ts +++ b/connection.ts @@ -352,6 +352,7 @@ export class RedisConnection const reply = await command.execute(); command.resolve(reply); } catch (error) { + console.error("failed", error); if ( !isRetriableError(error) || this.isManuallyClosedByUser() From f4f03965d9bc905a08dbdd32b9c8afec677feceb Mon Sep 17 00:00:00 2001 From: uki00a Date: Tue, 10 Dec 2024 23:17:55 +0900 Subject: [PATCH 4/4] fix --- connection.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/connection.ts b/connection.ts index 8179ff96..68b6b08d 100644 --- a/connection.ts +++ b/connection.ts @@ -352,7 +352,6 @@ export class RedisConnection const reply = await command.execute(); command.resolve(reply); } catch (error) { - console.error("failed", error); if ( !isRetriableError(error) || this.isManuallyClosedByUser() @@ -361,15 +360,18 @@ export class RedisConnection return command.reject(error); } + let backoff = 0; for (let i = 0; i < this.maxRetryCount; i++) { // Try to reconnect to the server and retry the command this.#close(true); try { + this.#dispatchEvent("reconnecting", { delay: backoff }); await this.connect(); const reply = await command.execute(); return command.resolve(reply); - } catch { // TODO: use `AggregateError`? - const backoff = this.backoff(i); + } catch (error) { + this.#dispatchEvent("error", { error }); // TODO: use `AggregateError`? + backoff = this.backoff(i); await delay(backoff); } }