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

chore: refactor onError handler

This commit is contained in:
Ruben Bridgewater
2017-05-28 04:33:33 +02:00
parent b837b46e76
commit 670550416e
2 changed files with 34 additions and 32 deletions

View File

@@ -10,6 +10,25 @@ const replyHandler = require('./replyHandler')
const onResult = replyHandler.onResult
const onError = replyHandler.onError
function onStreamError (client, err) {
if (client.closing) {
return
}
err.message = `Redis connection to ${client.address} failed - ${err.message}`
debug(err.message)
client.connected = false
client.ready = false
// Only emit the error if the retryStrategy option is not set
if (client.retryStrategyProvided === false) {
client.emit('error', 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)
}
/**
* @description Create a new Parser instance and pass all the necessary options to it
*
@@ -75,9 +94,11 @@ function connect (client) {
}
}
const stream = client._stream
if (client.options.connectTimeout) {
// TODO: Investigate why this is not properly triggered
client._stream.setTimeout(client.connectTimeout, () => {
stream.setTimeout(client.connectTimeout, () => {
// Note: This is only tested if a internet connection is established
reconnect(client, 'timeout')
})
@@ -85,36 +106,36 @@ function connect (client) {
/* 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'
client._stream.once(connectEvent, () => {
client._stream.removeAllListeners('timeout')
stream.once(connectEvent, () => {
stream.removeAllListeners('timeout')
client.timesConnected++
onConnect(client)
})
client._stream.on('data', (bufferFromSocket) => {
debug('Net read %s id %s', client.address, client.connectionId)
stream.on('data', (bufferFromSocket) => {
debug('Net read %s id %s: %s', client.address, client.connectionId, bufferFromSocket)
parser.execute(bufferFromSocket)
})
client._stream.on('error', (err) => {
client.onError(err)
stream.on('error', (err) => {
onStreamError(client, err)
})
/* istanbul ignore next: difficult to test and not important as long as we keep this listener */
client._stream.on('clientError', (err) => {
stream.on('clientError', (err) => {
debug('clientError occurred')
client.onError(err)
onStreamError(client, err)
})
client._stream.once('close', (hadError) => {
stream.once('close', (hadError) => {
reconnect(client, 'close')
})
client._stream.once('end', () => {
stream.once('end', () => {
reconnect(client, 'end')
})
client._stream.setNoDelay()
stream.setNoDelay()
// Fire the command before redis is connected to be sure it's the first fired command
if (client.authPass !== undefined) {