File tree 2 files changed +44
-0
lines changed
2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -385,6 +385,29 @@ class Cluster extends EventEmitter {
385
385
) ;
386
386
}
387
387
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
+
388
411
/**
389
412
* Get nodes with the specified role
390
413
*
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments