You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-04 15:02:09 +03:00
chore: refactor flush and error
This commit is contained in:
@@ -12,26 +12,41 @@ function onConnect (client) {
|
||||
client._stream.setKeepAlive(client.options.socketKeepalive)
|
||||
client._stream.setTimeout(0)
|
||||
|
||||
// TODO: Deprecate the connect event.
|
||||
client.emit('connect')
|
||||
client.initializeRetryVars()
|
||||
|
||||
if (client.options.noReadyCheck) {
|
||||
onReady(client)
|
||||
readyHandler(client)
|
||||
} else {
|
||||
readyCheck(client)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Empty the offline queue and call the commands
|
||||
*
|
||||
* @param {RedisClient} client
|
||||
*/
|
||||
function sendOfflineQueue (client) {
|
||||
while (client.offlineQueue.length) {
|
||||
const command = client.offlineQueue.shift()
|
||||
const queue = client.offlineQueue
|
||||
while (queue.length) {
|
||||
const command = queue.shift()
|
||||
debug('Sending offline command: %s', command.command)
|
||||
client.internalSendCommand(command)
|
||||
}
|
||||
}
|
||||
|
||||
function onReady (client) {
|
||||
debug('onReady called %s id %s', client.address, client.connectionId)
|
||||
/**
|
||||
* @description Transparently perform predefined commands and emit ready.
|
||||
*
|
||||
* Emit ready before the all commands returned.
|
||||
* The order of the commands is important.
|
||||
*
|
||||
* @param {RedisClient} client
|
||||
*/
|
||||
function readyHandler (client) {
|
||||
debug('readyHandler called %s id %s', client.address, client.connectionId)
|
||||
client.ready = true
|
||||
|
||||
client.cork = () => {
|
||||
@@ -50,7 +65,6 @@ function onReady (client) {
|
||||
client._stream.uncork()
|
||||
}
|
||||
|
||||
// Restore modal commands from previous connection. The order of the commands is important
|
||||
if (client.selectedDb !== undefined) {
|
||||
client.internalSendCommand(new Command('select', [client.selectedDb])).catch((err) => {
|
||||
if (!client.closing) {
|
||||
@@ -78,7 +92,7 @@ function onReady (client) {
|
||||
// // individual: function noop () {}
|
||||
// }
|
||||
if (!client.options.disableResubscribing && callbackCount) {
|
||||
debug('Sending pub/sub onReady commands')
|
||||
debug('Sending pub/sub commands')
|
||||
for (const key in client.subscriptionSet) {
|
||||
if (client.subscriptionSet.hasOwnProperty(key)) {
|
||||
const command = key.slice(0, key.indexOf('_'))
|
||||
@@ -95,55 +109,54 @@ function onReady (client) {
|
||||
client.emit('ready')
|
||||
}
|
||||
|
||||
function onInfoFail (client, err) {
|
||||
if (client.closing) {
|
||||
return
|
||||
}
|
||||
|
||||
if (err.message === "ERR unknown command 'info'") {
|
||||
onReady(client)
|
||||
return
|
||||
}
|
||||
err.message = `Ready check failed: ${err.message}`
|
||||
client.emit('error', err)
|
||||
return
|
||||
}
|
||||
|
||||
function onInfoCmd (client, res) {
|
||||
/* istanbul ignore if: some servers might not respond with any info data. client is just a safety check that is difficult to test */
|
||||
if (!res) {
|
||||
debug('The info command returned without any data.')
|
||||
onReady(client)
|
||||
return
|
||||
}
|
||||
|
||||
if (!client.serverInfo.loading || client.serverInfo.loading === '0') {
|
||||
// If the master_link_status exists but the link is not up, try again after 50 ms
|
||||
if (client.serverInfo.master_link_status && client.serverInfo.master_link_status !== 'up') {
|
||||
client.serverInfo.loading_eta_seconds = 0.05
|
||||
} else {
|
||||
// Eta loading should change
|
||||
debug('Redis server ready.')
|
||||
onReady(client)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var retryTime = +client.serverInfo.loading_eta_seconds * 1000
|
||||
if (retryTime > 1000) {
|
||||
retryTime = 1000
|
||||
}
|
||||
debug('Redis server still loading, trying again in %s', retryTime)
|
||||
setTimeout((client) => readyCheck(client), retryTime, client)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Perform a info command and check if Redis is ready
|
||||
*
|
||||
* @param {RedisClient} client
|
||||
*/
|
||||
function readyCheck (client) {
|
||||
debug('Checking server ready state...')
|
||||
// Always fire client info command as first command even if other commands are already queued up
|
||||
client.ready = true
|
||||
client.info()
|
||||
.then((res) => onInfoCmd(client, res))
|
||||
.catch((err) => onInfoFail(client, err))
|
||||
client.info().then((res) => {
|
||||
/* istanbul ignore if: some servers might not respond with any info data. client is just a safety check that is difficult to test */
|
||||
if (!res) {
|
||||
debug('The info command returned without any data.')
|
||||
readyHandler(client)
|
||||
return
|
||||
}
|
||||
|
||||
if (!client.serverInfo.loading || client.serverInfo.loading === '0') {
|
||||
// If the master_link_status exists but the link is not up, try again after 50 ms
|
||||
if (client.serverInfo.master_link_status && client.serverInfo.master_link_status !== 'up') {
|
||||
client.serverInfo.loading_eta_seconds = 0.05
|
||||
} else {
|
||||
// Eta loading should change
|
||||
debug('Redis server ready.')
|
||||
readyHandler(client)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
var retryTime = +client.serverInfo.loading_eta_seconds * 1000
|
||||
if (retryTime > 1000) {
|
||||
retryTime = 1000
|
||||
}
|
||||
debug('Redis server still loading, trying again in %s', retryTime)
|
||||
setTimeout((client) => readyCheck(client), retryTime, client)
|
||||
}).catch((err) => {
|
||||
if (client.closing) {
|
||||
return
|
||||
}
|
||||
|
||||
if (err.message === "ERR unknown command 'info'") {
|
||||
readyHandler(client)
|
||||
return
|
||||
}
|
||||
err.message = `Ready check failed: ${err.message}`
|
||||
client.emit('error', err)
|
||||
return
|
||||
})
|
||||
client.ready = false
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user