Skip to content

Commit

Permalink
fix(cluster): prefer master when there're two same node for a slot
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Jul 16, 2019
1 parent 6b67c3f commit 8fb9f97
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/cluster/ConnectionPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ export default class ConnectionPool extends EventEmitter {
debug("Reset with %O", nodes);
const newNodes = {};
nodes.forEach(node => {
newNodes[getNodeKey(node)] = node;
const key = getNodeKey(node);

// Don't override the existing (master) node
// when the current one is slave.
if (!(node.readOnly && newNodes[key])) {
newNodes[key] = node;
}
});

Object.keys(this.nodes.all).forEach(key => {
Expand Down
28 changes: 28 additions & 0 deletions test/unit/clusters/ConnectionPool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as sinon from "sinon";
import { expect } from "chai";
import ConnectionPool from "../../../lib/cluster/ConnectionPool";

describe("ConnectionPool", () => {
describe("#reset", () => {
it("prefers to master if there are two same node for a slot", () => {
const pool = new ConnectionPool({});
const stub = sinon.stub(pool, "findOrCreate");

pool.reset([
{ host: "127.0.0.1", port: 30001, readOnly: true },
{ host: "127.0.0.1", port: 30001, readOnly: false }
]);

expect(stub.callCount).to.eql(1);
expect(stub.firstCall.args[1]).to.eql(false);

pool.reset([
{ host: "127.0.0.1", port: 30001, readOnly: false },
{ host: "127.0.0.1", port: 30001, readOnly: true }
]);

expect(stub.callCount).to.eql(2);
expect(stub.firstCall.args[1]).to.eql(false);
});
});
});
4 changes: 2 additions & 2 deletions test/unit/cluster.ts → test/unit/clusters/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { nodeKeyToRedisOptions } from "../../lib/cluster/util";
import { Cluster } from "../../lib";
import { nodeKeyToRedisOptions } from "../../../lib/cluster/util";
import { Cluster } from "../../../lib";
import * as sinon from "sinon";
import { expect } from "chai";

Expand Down

0 comments on commit 8fb9f97

Please sign in to comment.