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 070599e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/cluster/ConnectionPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,10 @@ export default class ConnectionPool extends EventEmitter {
debug("Reset with %O", nodes);
const newNodes = {};
nodes.forEach(node => {
newNodes[getNodeKey(node)] = node;
const key = getNodeKey(node)
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(true);

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(true);
})
})
});
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 070599e

Please sign in to comment.