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

chore: add print helper again and refactor some code

Expose the RedisClient directly instead of only being a property
This commit is contained in:
Ruben Bridgewater
2017-05-29 18:23:24 +02:00
parent 4e593587cb
commit 0276e15f04
14 changed files with 169 additions and 104 deletions

View File

@@ -10,9 +10,9 @@ const replyHandler = require('./replyHandler')
const onResult = replyHandler.onResult
const onError = replyHandler.onError
var reconnect = function (client, why, err) {
reconnect = require('./reconnect')
reconnect(client, why, err)
var lazyReconnect = function (client, why, err) {
lazyReconnect = require('./reconnect')
lazyReconnect(client, why, err)
}
function onStreamError (client, err) {
@@ -31,7 +31,7 @@ function onStreamError (client, err) {
}
// 'error' events get turned into exceptions if they aren't listened for. If the user handled this error
// then we should try to reconnect.
reconnect(client, 'error', err)
lazyReconnect(client, 'error', err)
}
/**
@@ -91,9 +91,19 @@ function connect (client) {
client._stream.destroy()
}
/* istanbul ignore if: travis does not work with stunnel atm. Therefore the tls tests are skipped on travis */
if (client._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)
}
@@ -105,11 +115,10 @@ function connect (client) {
// TODO: Investigate why this is not properly triggered
stream.setTimeout(client._options.connectTimeout, () => {
// Note: This is only tested if a internet connection is established
reconnect(client, 'timeout')
lazyReconnect(client, 'timeout')
})
}
/* istanbul ignore next: travis does not work with stunnel atm. Therefore the tls tests are skipped on travis */
const connectEvent = client._options.tls ? 'secureConnect' : 'connect'
stream.once(connectEvent, () => {
stream.removeAllListeners('timeout')
@@ -126,18 +135,12 @@ function connect (client) {
onStreamError(client, err)
})
/* istanbul ignore next: difficult to test and not important as long as we keep this listener */
stream.on('clientError', (err) => {
debug('clientError occurred')
onStreamError(client, err)
})
stream.once('close', (hadError) => {
reconnect(client, 'close')
lazyReconnect(client, 'close')
})
stream.once('end', () => {
reconnect(client, 'end')
lazyReconnect(client, 'end')
})
stream.setNoDelay()