Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move epoch off of client object and to socket object #2718

Open
wants to merge 1 commit into
base: v5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/client/lib/client/index.ts
Expand Up @@ -282,9 +282,12 @@ export default class RedisClient<
private _self = this;
private _commandOptions?: CommandOptions<TYPE_MAPPING>;
#dirtyWatch?: string;
#epoch: number;
#watchEpoch?: number;

get socketEpoch(): number {
return this._self.#socket.epoch;
}

get options(): RedisClientOptions<M, F, S, RESP> | undefined {
return this._self.#options;
}
Expand Down Expand Up @@ -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<M, F, S, RESP, TYPE_MAPPING>): RedisClientOptions<M, F, S, RESP, TYPE_MAPPING> | undefined {
Expand Down Expand Up @@ -454,7 +456,6 @@ export default class RedisClient<
})
.on('connect', () => this.emit('connect'))
.on('ready', () => {
this.#epoch++;
this.emit('ready');
this.#setPingTimer();
this.#maybeScheduleWrite();
Expand Down Expand Up @@ -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<SimpleStringReply<'OK'>, TYPE_MAPPING>;
}

Expand Down Expand Up @@ -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');
}

Expand Down
7 changes: 7 additions & 0 deletions packages/client/lib/client/socket.ts
Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down