From 23ebdb53d312c651b20ae1bebb70e6ec1644a524 Mon Sep 17 00:00:00 2001 From: Shaya Potter Date: Thu, 14 Mar 2024 10:35:36 +0200 Subject: [PATCH] move epoch off of client object and to socket object --- packages/client/lib/client/index.ts | 11 ++++++----- packages/client/lib/client/socket.ts | 7 +++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/client/lib/client/index.ts b/packages/client/lib/client/index.ts index 3efa793eeb..7634c9c40b 100644 --- a/packages/client/lib/client/index.ts +++ b/packages/client/lib/client/index.ts @@ -282,9 +282,12 @@ export default class RedisClient< private _self = this; private _commandOptions?: CommandOptions; #dirtyWatch?: string; - #epoch: number; #watchEpoch?: number; + get socketEpoch(): number { + return this._self.#socket.epoch; + } + get options(): RedisClientOptions | undefined { return this._self.#options; } @@ -314,7 +317,6 @@ export default class RedisClient< this.#options = this.#initiateOptions(options); this.#queue = this.#initiateQueue(); this.#socket = this.#initiateSocket(); - this.#epoch = 0; } #initiateOptions(options?: RedisClientOptions): RedisClientOptions | undefined { @@ -454,7 +456,6 @@ export default class RedisClient< }) .on('connect', () => this.emit('connect')) .on('ready', () => { - this.#epoch++; this.emit('ready'); this.#setPingTimer(); this.#maybeScheduleWrite(); @@ -724,7 +725,7 @@ export default class RedisClient< const reply = await this._self.sendCommand( pushVariadicArguments(['WATCH'], key) ); - this._self.#watchEpoch ??= this._self.#epoch; + this._self.#watchEpoch ??= this.socketEpoch; return reply as unknown as ReplyWithTypeMapping, TYPE_MAPPING>; } @@ -827,7 +828,7 @@ export default class RedisClient< throw new WatchError(dirtyWatch); } - if (watchEpoch && watchEpoch !== this._self.#epoch) { + if (watchEpoch && watchEpoch !== this.socketEpoch) { throw new WatchError('Client reconnected after WATCH'); } diff --git a/packages/client/lib/client/socket.ts b/packages/client/lib/client/socket.ts index dcadad4c3d..8a9bd0ee26 100644 --- a/packages/client/lib/client/socket.ts +++ b/packages/client/lib/client/socket.ts @@ -72,6 +72,12 @@ export default class RedisSocket extends EventEmitter { #isSocketUnrefed = false; + #epoch = 0; + + get epoch() { + return this.#epoch; + } + constructor(initiator: RedisSocketInitiator, options?: RedisSocketOptions) { super(); @@ -212,6 +218,7 @@ export default class RedisSocket extends EventEmitter { throw err; } this.#isReady = true; + this.#epoch++; this.emit('ready'); } catch (err) { const retryIn = this.#shouldReconnect(retries++, err as Error);