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 (#1497)
  • Loading branch information
marcbachmann authored Feb 6, 2022
1 parent c72f242 commit f357a31
Show file tree
Hide file tree
Showing 2 changed files with 42 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 @@ -323,6 +323,10 @@ Redis.prototype.connect = function (callback) {
// 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 = {};
Expand Down
38 changes: 38 additions & 0 deletions test/functional/pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,44 @@ describe("pipeline", function () {
})
.catch(done);
});

it("should reload scripts on redis restart (reconnect)", async function () {
const redis = new Redis({ connectionName: "load-script-on-reconnect" });
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 client = await redis.client("list").then((clients) => {
const myInfo = clients
.split("\n")
.find((client) => client.includes("load-script-on-reconnect"));

const match = / addr=([^ ]+)/.exec(myInfo);
if (match) return match[1];
});

await redis2.script("flush");
await redis2.client("kill", "addr", client);

// 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 f357a31

Please sign in to comment.