Skip to content

Commit

Permalink
fix: Reset loaded script hashes to force a reload of scripts after re…
Browse files Browse the repository at this point in the history
…connect of redis
  • Loading branch information
marcbachmann authored and luin committed Mar 14, 2022
1 parent 1e10c95 commit 60c2af9
Showing 1 changed file with 10 additions and 32 deletions.
42 changes: 10 additions & 32 deletions lib/Redis.ts
Expand Up @@ -72,12 +72,6 @@ class Redis extends Commander {
private _autoPipelines = new Map();
private _runningAutoPipelines = new Set();

// Prepare a cache of scripts and setup a interval which regularly clears it
private _addedScriptHashes: { [sha: string]: true } = {};
private _addedScriptHashesCleanInterval?: ReturnType<
typeof setInterval
> | null = null;

constructor(port: number, host: string, options: RedisOptions);
constructor(path: string, options: RedisOptions);
constructor(port: number, options: RedisOptions);
Expand Down Expand Up @@ -187,13 +181,6 @@ class Redis extends Commander {
process.nextTick(this.emit.bind(this, status, arg));
}

private clearAddedScriptHashesCleanInterval() {
if (this._addedScriptHashesCleanInterval) {
clearInterval(this._addedScriptHashesCleanInterval);
this._addedScriptHashesCleanInterval = null;
}
}

/**
* Create a connection to Redis.
* This method will be invoked automatically when creating a new Redis instance
Expand All @@ -213,18 +200,6 @@ class Redis extends Commander {
return;
}

// Make sure only one timer is active at a time
this.clearAddedScriptHashesCleanInterval();

// Scripts need to get reset on reconnect as redis
// might have been restarted or some failover happened
this._addedScriptHashes = {};

// Start the script cache cleaning
this._addedScriptHashesCleanInterval = setInterval(() => {
this._addedScriptHashes = {};
}, this.options.maxScriptsCachingTime);

this.connectionEpoch += 1;
this.setStatus("connecting");

Expand Down Expand Up @@ -343,8 +318,6 @@ class Redis extends Commander {
* If you want to wait for the pending replies, use Redis#quit instead.
*/
disconnect(reconnect = false) {
this.clearAddedScriptHashesCleanInterval();

if (!reconnect) {
this.manuallyClosing = true;
}
Expand Down Expand Up @@ -626,10 +599,6 @@ class Redis extends Commander {
command.setTimeout(this.options.commandTimeout);
}

if (command.name === "quit") {
this.clearAddedScriptHashesCleanInterval();
}

let writable =
this.status === "ready" ||
(!stream &&
Expand Down Expand Up @@ -677,7 +646,16 @@ class Redis extends Commander {
command.args
);
}
(stream || this.stream).write(command.toWritable());

if (stream) {
if (stream.isPipeline) {
stream.write(command.toWritable(stream.destination.redis.stream));
} else {
stream.write(command.toWritable(stream));
}
} else {
this.stream.write(command.toWritable(this.stream));
}

this.commandQueue.push({
command: command,
Expand Down

0 comments on commit 60c2af9

Please sign in to comment.