You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
chore: refactor onError handler
This commit is contained in:
21
index.js
21
index.js
@@ -156,25 +156,6 @@ RedisClient.prototype.flushAndError = function (message, code, options) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RedisClient.prototype.onError = function (err) {
|
|
||||||
if (this.closing) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err.message = `Redis connection to ${this.address} failed - ${err.message}`
|
|
||||||
debug(err.message)
|
|
||||||
this.connected = false
|
|
||||||
this.ready = false
|
|
||||||
|
|
||||||
// Only emit the error if the retryStrategy option is not set
|
|
||||||
if (this.retryStrategyProvided === false) {
|
|
||||||
this.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(this, 'error', err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do not call internalSendCommand directly, if you are not absolutely certain it handles everything properly
|
// Do not call internalSendCommand directly, if you are not absolutely certain it handles everything properly
|
||||||
// e.g. monitor / info does not work with internalSendCommand only
|
// e.g. monitor / info does not work with internalSendCommand only
|
||||||
RedisClient.prototype.internalSendCommand = function (commandObj) {
|
RedisClient.prototype.internalSendCommand = function (commandObj) {
|
||||||
@@ -213,7 +194,7 @@ module.exports = {
|
|||||||
debugMode: /\bredis\b/i.test(process.env.NODE_DEBUG),
|
debugMode: /\bredis\b/i.test(process.env.NODE_DEBUG),
|
||||||
RedisClient,
|
RedisClient,
|
||||||
Multi,
|
Multi,
|
||||||
AbortError: errorClasses.AbortError,
|
AbortError: Errors.AbortError,
|
||||||
ParserError: Errors.ParserError,
|
ParserError: Errors.ParserError,
|
||||||
RedisError: Errors.RedisError,
|
RedisError: Errors.RedisError,
|
||||||
ReplyError: Errors.ReplyError,
|
ReplyError: Errors.ReplyError,
|
||||||
|
@@ -10,6 +10,25 @@ const replyHandler = require('./replyHandler')
|
|||||||
const onResult = replyHandler.onResult
|
const onResult = replyHandler.onResult
|
||||||
const onError = replyHandler.onError
|
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
|
* @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) {
|
if (client.options.connectTimeout) {
|
||||||
// TODO: Investigate why this is not properly triggered
|
// 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
|
// Note: This is only tested if a internet connection is established
|
||||||
reconnect(client, 'timeout')
|
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 */
|
/* 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'
|
const connectEvent = client.options.tls ? 'secureConnect' : 'connect'
|
||||||
client._stream.once(connectEvent, () => {
|
stream.once(connectEvent, () => {
|
||||||
client._stream.removeAllListeners('timeout')
|
stream.removeAllListeners('timeout')
|
||||||
client.timesConnected++
|
client.timesConnected++
|
||||||
onConnect(client)
|
onConnect(client)
|
||||||
})
|
})
|
||||||
|
|
||||||
client._stream.on('data', (bufferFromSocket) => {
|
stream.on('data', (bufferFromSocket) => {
|
||||||
debug('Net read %s id %s', client.address, client.connectionId)
|
debug('Net read %s id %s: %s', client.address, client.connectionId, bufferFromSocket)
|
||||||
parser.execute(bufferFromSocket)
|
parser.execute(bufferFromSocket)
|
||||||
})
|
})
|
||||||
|
|
||||||
client._stream.on('error', (err) => {
|
stream.on('error', (err) => {
|
||||||
client.onError(err)
|
onStreamError(client, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
/* istanbul ignore next: difficult to test and not important as long as we keep this listener */
|
/* 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')
|
debug('clientError occurred')
|
||||||
client.onError(err)
|
onStreamError(client, err)
|
||||||
})
|
})
|
||||||
|
|
||||||
client._stream.once('close', (hadError) => {
|
stream.once('close', (hadError) => {
|
||||||
reconnect(client, 'close')
|
reconnect(client, 'close')
|
||||||
})
|
})
|
||||||
|
|
||||||
client._stream.once('end', () => {
|
stream.once('end', () => {
|
||||||
reconnect(client, '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
|
// Fire the command before redis is connected to be sure it's the first fired command
|
||||||
if (client.authPass !== undefined) {
|
if (client.authPass !== undefined) {
|
||||||
|
Reference in New Issue
Block a user