1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

chore: minor refactoring

This commit is contained in:
Ruben Bridgewater
2017-05-30 00:44:57 +02:00
parent 0276e15f04
commit 265ce48af4
6 changed files with 50 additions and 40 deletions

View File

@@ -76,14 +76,15 @@ function createParser (client) {
function connect (client) {
// Init parser
const parser = createParser(client)
const options = client._options
client._replyParser = parser
if (client._options.stream) {
if (options.stream) {
// Only add the listeners once in case of a reconnect try (that won't work)
if (client._stream) {
return
}
client._stream = client._options.stream
client._stream = options.stream
} else {
// On a reconnect destroy the former stream and retry
if (client._stream) {
@@ -91,19 +92,8 @@ function connect (client) {
client._stream.destroy()
}
if (client._options.tls) {
if (options.tls) {
client._stream = tls.connect(client._connectionOptions)
// Whenever a handshake times out.
// Older Node.js versions use "clientError", newer versions use tlsClientError.
stream.once('clientError', (err) => {
debug('clientError occurred')
onStreamError(client, err)
})
stream.once('tlsClientError', (err) => {
debug('clientError occurred')
onStreamError(client, err)
})
} else {
client._stream = net.createConnection(client._connectionOptions)
}
@@ -111,18 +101,18 @@ function connect (client) {
const stream = client._stream
if (client._options.connectTimeout) {
if (options.connectTimeout) {
// TODO: Investigate why this is not properly triggered
stream.setTimeout(client._options.connectTimeout, () => {
stream.setTimeout(client._connectTimeout, () => {
// Note: This is only tested if a internet connection is established
lazyReconnect(client, 'timeout')
})
}
const connectEvent = client._options.tls ? 'secureConnect' : 'connect'
const connectEvent = options.tls ? 'secureConnect' : 'connect'
stream.once(connectEvent, () => {
stream.removeAllListeners('timeout')
client.timesConnected++
client._timesConnected++
onConnect(client)
})
@@ -143,14 +133,27 @@ function connect (client) {
lazyReconnect(client, 'end')
})
if (options.tls) {
// Whenever a handshake times out.
// Older Node.js versions use "clientError", newer versions use tlsClientError.
stream.once('clientError', (err) => {
debug('clientError occurred')
onStreamError(client, err)
})
stream.once('tlsClientError', (err) => {
debug('clientError occurred')
onStreamError(client, err)
})
}
stream.setNoDelay()
// Fire the command before redis is connected to be sure it's the first fired command.
// TODO: Consider calling the ready check before Redis is connected as well.
// That could improve the ready performance. Measure the rough time difference!
if (client._options.password !== undefined) {
if (options.password !== undefined) {
client.ready = true
client.auth(client._options.password).catch((err) => {
client.auth(options.password).catch((err) => {
client._closing = true
process.nextTick(() => {
client.emit('error', err)