Skip to content

Commit 8b150c2

Browse files
SGrondinluin
authored andcommitted
feat(cluster): add #duplicate() method (#926)
1 parent 90b1aec commit 8b150c2

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/cluster/index.ts

+23
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,29 @@ class Cluster extends EventEmitter {
385385
);
386386
}
387387

388+
/**
389+
* Create a new instance with the same startup nodes and options as the current one.
390+
*
391+
* @example
392+
* ```js
393+
* var cluster = new Redis.Cluster([{ host: "127.0.0.1", port: "30001" }]);
394+
* var anotherCluster = cluster.duplicate();
395+
* ```
396+
*
397+
* @public
398+
* @param {(Array<string | number | object>)} [overrideStartupNodes=[]]
399+
* @param {IClusterOptions} [overrideOptions={}]
400+
* @memberof Cluster
401+
*/
402+
public duplicate(overrideStartupNodes = [], overrideOptions = {}) {
403+
const startupNodes =
404+
overrideStartupNodes.length > 0
405+
? overrideStartupNodes
406+
: this.startupNodes.slice(0);
407+
const options = Object.assign({}, this.options, overrideOptions);
408+
return new Cluster(startupNodes, options);
409+
}
410+
388411
/**
389412
* Get nodes with the specified role
390413
*

test/functional/cluster/duplicate.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import MockServer from "../../helpers/mock_server";
2+
import { Cluster } from "../../../lib";
3+
import { expect } from "chai";
4+
5+
describe("cluster:duplicate", () => {
6+
it("clone the options", done => {
7+
var node = new MockServer(30001);
8+
var cluster = new Cluster([]);
9+
var duplicatedCluster = cluster.duplicate([
10+
{ host: "127.0.0.1", port: "30001" }
11+
]);
12+
13+
node.once("connect", function() {
14+
expect(duplicatedCluster.nodes()).to.have.lengthOf(1);
15+
expect(duplicatedCluster.nodes()[0].options.port).to.eql(30001);
16+
cluster.disconnect();
17+
duplicatedCluster.disconnect();
18+
done();
19+
});
20+
});
21+
});

0 commit comments

Comments
 (0)