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 committed Jan 31, 2022
1 parent bf3bec7 commit 94bafe5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/redis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,10 @@ Redis.prototype.connect = function (callback) {
// Make sure only one timer is active at a time
clearInterval(this._addedScriptHashesCleanInterval);

// 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 = {};
Expand Down
33 changes: 33 additions & 0 deletions test/functional/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,39 @@ describe("pipeline", function () {
})
.catch(done);
});

it("should reload scripts on redis restart (reconnect)", async function () {
const redis = new Redis();
const redis2 = new Redis();
redis.defineCommand("exeecafterreconnect", {
numberOfKeys: 0,
lua: `return "OK"`,
});

const [[err, res]] = await redis.multi([["exeecafterreconnect"]]).exec();
expect(err).to.equal(null);
expect(res).to.equal("OK");

const clientId = await redis.client("id");
await redis2
.pipeline([
["script", "flush"],
["client", "kill", "id", clientId],
])
.exec();

// Wait for reconnect, at the moment scripts are not loaded
// if the pipeline starts before ioredis reconnects
await redis.ping();

const [[err2, res2]] = await redis
.multi([["exeecafterreconnect"]])
.exec();
expect(err2).to.equal(null);
expect(res2).to.equal("OK");
redis.disconnect();
redis2.disconnect();
});
});

describe("#length", function () {
Expand Down

0 comments on commit 94bafe5

Please sign in to comment.