Pretty format (#20352)
* Update prettier flow to include JS * Run prettier * ...run prettier
This commit is contained in:
@@ -9,7 +9,7 @@ const redisMaxDb = REDIS_MAX_DB || 15
|
||||
// Maximum delay between reconnection attempts after backoff
|
||||
const maxReconnectDelay = 5000
|
||||
|
||||
function formatRedisError (error) {
|
||||
function formatRedisError(error) {
|
||||
const errorCode = error ? error.code : null
|
||||
const errorName = error ? error.constructor.name : 'Server disconnection'
|
||||
const errorMsg = error ? error.toString() : 'unknown (commonly a server idle timeout)'
|
||||
@@ -17,7 +17,7 @@ function formatRedisError (error) {
|
||||
return preamble + ': ' + errorMsg
|
||||
}
|
||||
|
||||
export default function createClient (options = {}) {
|
||||
export default function createClient(options = {}) {
|
||||
const { db, name, url } = options
|
||||
|
||||
// If no Redis URL is provided, bail out
|
||||
@@ -29,13 +29,15 @@ export default function createClient (options = {}) {
|
||||
if (db != null) {
|
||||
if (!Number.isInteger(db) || db < redisMinDb || db > redisMaxDb) {
|
||||
throw new TypeError(
|
||||
`Redis database number must be an integer between ${redisMinDb} and ${redisMaxDb} but was: ${JSON.stringify(db)}`
|
||||
`Redis database number must be an integer between ${redisMinDb} and ${redisMaxDb} but was: ${JSON.stringify(
|
||||
db
|
||||
)}`
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
let pingInterval = null
|
||||
function stopPinging () {
|
||||
function stopPinging() {
|
||||
if (pingInterval) {
|
||||
clearInterval(pingInterval)
|
||||
pingInterval = null
|
||||
@@ -46,12 +48,12 @@ export default function createClient (options = {}) {
|
||||
const client = Redis.createClient(url, {
|
||||
// Only add this configuration for TLS-enabled Redis URL values.
|
||||
// Otherwise, it breaks for local Redis instances without TLS enabled.
|
||||
...url.startsWith('rediss://') && {
|
||||
...(url.startsWith('rediss://') && {
|
||||
tls: {
|
||||
// Required for production Heroku Redis
|
||||
rejectUnauthorized: false
|
||||
}
|
||||
},
|
||||
rejectUnauthorized: false,
|
||||
},
|
||||
}),
|
||||
|
||||
// Any running command that is unfulfilled when a connection is lost should
|
||||
// NOT be retried after the connection has been reestablished.
|
||||
@@ -73,26 +75,25 @@ export default function createClient (options = {}) {
|
||||
// Be aware that this retry (NOT just reconnection) strategy appears to
|
||||
// be a major point of confusion (and possibly legitimate issues) between
|
||||
// reconnecting and retrying failed commands.
|
||||
retry_strategy:
|
||||
function ({
|
||||
attempt,
|
||||
error,
|
||||
total_retry_time: totalRetryTime,
|
||||
times_connected: timesConnected
|
||||
}) {
|
||||
let delayPerAttempt = 100
|
||||
retry_strategy: function ({
|
||||
attempt,
|
||||
error,
|
||||
total_retry_time: totalRetryTime,
|
||||
times_connected: timesConnected,
|
||||
}) {
|
||||
let delayPerAttempt = 100
|
||||
|
||||
// If the server appears to be unavailable, slow down faster
|
||||
if (error && error.code === 'ECONNREFUSED') {
|
||||
delayPerAttempt *= 5
|
||||
}
|
||||
// If the server appears to be unavailable, slow down faster
|
||||
if (error && error.code === 'ECONNREFUSED') {
|
||||
delayPerAttempt *= 5
|
||||
}
|
||||
|
||||
// Reconnect after delay
|
||||
return Math.min(attempt * delayPerAttempt, maxReconnectDelay)
|
||||
},
|
||||
// Reconnect after delay
|
||||
return Math.min(attempt * delayPerAttempt, maxReconnectDelay)
|
||||
},
|
||||
|
||||
// Expand whatever other options and overrides were provided
|
||||
...options
|
||||
...options,
|
||||
})
|
||||
|
||||
// Handle connection errors to prevent killing the Node.js process
|
||||
@@ -115,10 +116,9 @@ export default function createClient (options = {}) {
|
||||
|
||||
// Start pinging the server once per minute to prevent Redis connection
|
||||
// from closing when its idle `timeout` configuration value expires
|
||||
pingInterval = setInterval(
|
||||
() => { client.ping(() => {}) },
|
||||
60 * 1000
|
||||
)
|
||||
pingInterval = setInterval(() => {
|
||||
client.ping(() => {})
|
||||
}, 60 * 1000)
|
||||
})
|
||||
|
||||
client.on('end', () => {
|
||||
@@ -130,9 +130,15 @@ export default function createClient (options = {}) {
|
||||
const logPrefix = '[redis' + (name ? ` (${name})` : '') + ']'
|
||||
|
||||
// Add event listeners for basic logging
|
||||
client.on('connect', () => { console.log(logPrefix, 'Connection opened') })
|
||||
client.on('ready', () => { console.log(logPrefix, 'Ready to receive commands') })
|
||||
client.on('end', () => { console.log(logPrefix, 'Connection closed') })
|
||||
client.on('connect', () => {
|
||||
console.log(logPrefix, 'Connection opened')
|
||||
})
|
||||
client.on('ready', () => {
|
||||
console.log(logPrefix, 'Ready to receive commands')
|
||||
})
|
||||
client.on('end', () => {
|
||||
console.log(logPrefix, 'Connection closed')
|
||||
})
|
||||
client.on(
|
||||
'reconnecting',
|
||||
({
|
||||
@@ -141,7 +147,7 @@ export default function createClient (options = {}) {
|
||||
// The rest are unofficial properties but currently supported
|
||||
error,
|
||||
total_retry_time: totalRetryTime,
|
||||
times_connected: timesConnected
|
||||
times_connected: timesConnected,
|
||||
}) => {
|
||||
console.log(
|
||||
logPrefix,
|
||||
@@ -154,8 +160,12 @@ export default function createClient (options = {}) {
|
||||
)
|
||||
}
|
||||
)
|
||||
client.on('warning', (msg) => { console.warn(logPrefix, 'Warning:', msg) })
|
||||
client.on('error', (error) => { console.error(logPrefix, formatRedisError(error)) })
|
||||
client.on('warning', (msg) => {
|
||||
console.warn(logPrefix, 'Warning:', msg)
|
||||
})
|
||||
client.on('error', (error) => {
|
||||
console.error(logPrefix, formatRedisError(error))
|
||||
})
|
||||
|
||||
return client
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user