Skip to content

Commit

Permalink
fix: support parallel script execution in pipelines (#1304)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcbachmann committed Mar 21, 2021
1 parent 6cfae7e commit c917719
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/pipeline.ts
Expand Up @@ -313,12 +313,15 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {

const script = this._shaToScript[item.args[0]];

if (!script || this.redis._addedScriptHashes[script.sha]) {
if (
!script ||
this.redis._addedScriptHashes[script.sha] ||
scripts.includes(script)
) {
continue;
}

scripts.push(script);
this.redis._addedScriptHashes[script.sha] = true;
}

const _this = this;
Expand All @@ -330,7 +333,12 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {
if (this.isCluster) {
return pMap(scripts, (script) => _this.redis.script("load", script.lua), {
concurrency: 10,
}).then(execPipeline);
}).then(function () {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
return execPipeline();
});
}

return this.redis
Expand All @@ -352,7 +360,12 @@ Pipeline.prototype.exec = function (callback: CallbackFunction) {
})
);
})
.then(execPipeline);
.then(function () {
for (let i = 0; i < scripts.length; i++) {
_this.redis._addedScriptHashes[scripts[i].sha] = true;
}
return execPipeline();
});

function execPipeline() {
let data = "";
Expand Down
22 changes: 22 additions & 0 deletions test/functional/pipeline.ts
Expand Up @@ -320,6 +320,28 @@ describe("pipeline", function () {
});
});
});

it("should support parallel script execution", function (done) {
const random = `${Math.random()}`;
const redis = new Redis();
redis.defineCommand("something", {
numberOfKeys: 0,
lua: `return "${random}"`,
});
Promise.all([
redis.multi([["something"]]).exec(),
redis.multi([["something"]]).exec(),
])
.then(([[first], [second]]) => {
expect(first[0]).to.equal(null);
expect(first[1]).to.equal(random);
expect(second[0]).to.equal(null);
expect(second[1]).to.equal(random);
redis.disconnect();
done();
})
.catch(done);
});
});

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

0 comments on commit c917719

Please sign in to comment.