Skip to content

Commit

Permalink
docs(cluster): add doc for Cluster#nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Feb 7, 2016
1 parent 8064281 commit cefe973
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
20 changes: 20 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ Create a Redis instance

* [Cluster](#Cluster) ⇐ <code>[EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter)</code>
* [new Cluster(startupNodes, options)](#new_Cluster_new)
* [.connect()](#Cluster+connect) ⇒ <code>Promise</code>
* [.disconnect()](#Cluster+disconnect)
* [.nodes([role])](#Cluster+nodes) ⇒ <code>[Array.&lt;Redis&gt;](#Redis)</code>
* [.getBuiltinCommands()](#Commander+getBuiltinCommands) ⇒ <code>Array.&lt;string&gt;</code>
* [.createBuiltinCommand(commandName)](#Commander+createBuiltinCommand) ⇒ <code>object</code>
* [.defineCommand(name, definition)](#Commander+defineCommand)
Expand All @@ -220,12 +222,30 @@ Creates a Redis Cluster instance
| [options.retryDelayOnFailover] | <code>number</code> | <code>2000</code> | When an error is received when sending a command(e.g. "Connection is closed." when the target Redis node is down), |
| [options.retryDelayOnClusterDown] | <code>number</code> | <code>1000</code> | When a CLUSTERDOWN error is received, client will retry if `retryDelayOnClusterDown` is valid delay time. |

<a name="Cluster+connect"></a>
### cluster.connect() ⇒ <code>Promise</code>
Connect to a cluster

**Kind**: instance method of <code>[Cluster](#Cluster)</code>
**Access:** public
<a name="Cluster+disconnect"></a>
### cluster.disconnect()
Disconnect from every node in the cluster.

**Kind**: instance method of <code>[Cluster](#Cluster)</code>
**Access:** public
<a name="Cluster+nodes"></a>
### cluster.nodes([role]) ⇒ <code>[Array.&lt;Redis&gt;](#Redis)</code>
Get nodes with the specified role

**Kind**: instance method of <code>[Cluster](#Cluster)</code>
**Returns**: <code>[Array.&lt;Redis&gt;](#Redis)</code> - array of nodes
**Access:** public

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [role] | <code>string</code> | <code>&quot;\&quot;all\&quot;&quot;</code> | role, "masters", "slaves" or "all" |

<a name="Commander+getBuiltinCommands"></a>
### cluster.getBuiltinCommands() ⇒ <code>Array.&lt;string&gt;</code>
Return supported builtin commands
Expand Down
42 changes: 36 additions & 6 deletions lib/cluster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ Cluster.prototype.resetClusterDownQueue = function () {
this.clusterDownQueue = new Deque();
};

/**
* Connect to a cluster
*
* @return {Promise}
* @public
*/
Cluster.prototype.connect = function () {
return new Promise(function (resolve, reject) {
if (this.status === 'connecting' || this.status === 'connect' || this.status === 'ready') {
Expand Down Expand Up @@ -206,16 +212,28 @@ Cluster.prototype.disconnect = function (reconnect) {
});
};

Cluster.prototype.nodes = function (type) {
if (!type) {
type = 'all';
/**
* Get nodes with the specified role
*
* @param {string} [role="all"] - role, "masters", "slaves" or "all"
* @return {Redis[]} array of nodes
* @public
*/
Cluster.prototype.nodes = function (role) {
if (!role) {
role = 'all';
}
if (type !== 'all' && type !== 'masters' && type !== 'slaves') {
throw new Error('Invalid type "' + type + '". Expected "all", "masters" or "slaves"');
if (role !== 'all' && role !== 'masters' && role !== 'slaves') {
throw new Error('Invalid role "' + role + '". Expected "all", "masters" or "slaves"');
}
return _.values(this.connectionPool[type === 'all' ? 'nodes' : type]);
return _.values(this.connectionPool[role === 'all' ? 'nodes' : role]);
};

/**
* Select a subscriber from the cluster
*
* @private
*/
Cluster.prototype.selectSubscriber = function () {
this.subscriber = _.sample(this.connectionPool.nodes);
if (!this.subscriber) {
Expand Down Expand Up @@ -263,12 +281,24 @@ Cluster.prototype.selectSubscriber = function () {
});
};

/**
* Change cluster instance's status
*
* @param {string} status
* @private
*/
Cluster.prototype.setStatus = function (status) {
debug('status: %s -> %s', this.status || '[empty]', status);
this.status = status;
process.nextTick(this.emit.bind(this, status));
};

/**
* Refresh the slot cache
*
* @param {function} callback
* @private
*/
Cluster.prototype.refreshSlotsCache = function (callback) {
if (this.isRefreshing) {
if (typeof callback === 'function') {
Expand Down

0 comments on commit cefe973

Please sign in to comment.