Skip to content

Commit

Permalink
refactor: migrate cluster-related code to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
luin committed Oct 9, 2018
1 parent 80f4a45 commit 52982fb
Show file tree
Hide file tree
Showing 10 changed files with 740 additions and 33 deletions.
44 changes: 16 additions & 28 deletions lib/cluster/ConnectionPool.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {parseURL} from '../utils'
import {EventEmitter} from 'events'
import {sample} from '../utils'
import {noop, defaults} from '../utils/lodash'
import {IRedisOptions, getNodeKey} from './util'
import {IRedisOptions, getNodeKey, NodeKey, NodeRole} from './util'

const Redis = require('../redis')
const debug = require('../utils/debug')('ioredis:cluster:connectionPool')
Expand All @@ -22,11 +22,21 @@ export default class ConnectionPool extends EventEmitter {
super()
}

public getNodes(role: 'all' | 'master' | 'slave' = 'all'): any[] {
public getNodes(role: NodeRole = 'all'): any[] {
const nodes = this.nodes[role]
return Object.keys(nodes).map((key) => nodes[key])
}

public getInstanceByKey(key: NodeKey): any {
return this.nodes.all[key]
}

public getSampleInstance(role: NodeRole): any {
const keys = Object.keys(this.nodes[role])
const sampleKey = sample(keys)
return this.nodes[role][sampleKey]
}

/**
* Find or create a connection to the node
*
Expand All @@ -36,7 +46,6 @@ export default class ConnectionPool extends EventEmitter {
* @memberof ConnectionPool
*/
public findOrCreate(node: IRedisOptions, readOnly: boolean = false): any {
fillDefaultOptions(node)
const key = getNodeKey(node)
readOnly = Boolean(readOnly)

Expand Down Expand Up @@ -104,28 +113,12 @@ export default class ConnectionPool extends EventEmitter {
* @param {(Array<string | number | object>)} nodes
* @memberof ConnectionPool
*/
public reset(nodes: Array<string | number | object>): void {
public reset(nodes: IRedisOptions[]): void {
debug('Reset with %O', nodes);
const newNodes = {}
nodes.forEach((node) => {
const options: IRedisOptions = {}
if (typeof node === 'object') {
Object.assign(options, node)
} else if (typeof node === 'string') {
Object.assign(options, parseURL(node))
} else if (typeof node === 'number') {
options.port = node
} else {
throw new Error('Invalid argument ' + node)
}
if (typeof options.port === 'string') {
options.port = parseInt(options.port, 10)
}
delete options.db

fillDefaultOptions(options)
newNodes[getNodeKey(options)] = options
}, this)
newNodes[getNodeKey(node)] = node
})

Object.keys(this.nodes.all).forEach((key) => {
if (!newNodes[key]) {
Expand All @@ -139,8 +132,3 @@ export default class ConnectionPool extends EventEmitter {
})
}
}

function fillDefaultOptions(node: IRedisOptions): void {
node.port = node.port || 6379
node.host = node.host || '127.0.0.1'
}
Loading

0 comments on commit 52982fb

Please sign in to comment.