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

Fix script loading behavior on script flush with pipelines #1497

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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