Redis (ioredis) - 无法捕获连接错误以便优雅地处理它们
Redis (ioredis) - Unable to catch connection error in order to handle them gracefully
我正在尝试优雅地处理 Redis 错误,以便绕过错误并执行其他操作,而不是让我的应用程序崩溃。
但到目前为止,我不能只捕获 ioredis 抛出的异常,它绕过了我的 try/catch
并终止了当前进程。当前的行为不允许我优雅地处理错误,也不允许我从替代系统(而不是 redis)获取数据。
import { createLogger } from '@unly/utils-simple-logger';
import Redis from 'ioredis';
import epsagon from './epsagon';
const logger = createLogger({
label: 'Redis client',
});
/**
* Creates a redis client
*
* @param url Url of the redis client, must contain the port number and be of the form "localhost:6379"
* @param password Password of the redis client
* @param maxRetriesPerRequest By default, all pending commands will be flushed with an error every 20 retry attempts.
* That makes sure commands won't wait forever when the connection is down.
* Set to null to disable this behavior, and every command will wait forever until the connection is alive again.
* @return {Redis}
*/
export const getClient = (url = process.env.REDIS_URL, password = process.env.REDIS_PASSWORD, maxRetriesPerRequest = 20) => {
const client = new Redis(`redis://${url}`, {
password,
showFriendlyErrorStack: true, // See https://github.com/luin/ioredis#error-handling
lazyConnect: true, // XXX Don't attempt to connect when initializing the client, in order to properly handle connection failure on a use-case basis
maxRetriesPerRequest,
});
client.on('connect', function () {
logger.info('Connected to redis instance');
});
client.on('ready', function () {
logger.info('Redis instance is ready (data loaded from disk)');
});
// Handles redis connection temporarily going down without app crashing
// If an error is handled here, then redis will attempt to retry the request based on maxRetriesPerRequest
client.on('error', function (e) {
logger.error(`Error connecting to redis: "${e}"`);
epsagon.setError(e);
if (e.message === 'ERR invalid password') {
logger.error(`Fatal error occurred "${e.message}". Stopping server.`);
throw e; // Fatal error, don't attempt to fix
}
});
return client;
};
我正在模拟一个错误的 password/url 以查看配置错误时 redis 的反应。我已将 lazyConnect
设置为 true
以处理调用者的错误。
但是,当我将 url 定义为 localhoste:6379
(而不是 localhost:6379
) 时,出现以下错误:
server 2019-08-10T19:44:00.926Z [Redis client] error: Error connecting to redis: "Error: getaddrinfo ENOTFOUND localhoste localhoste:6379"
(x 20)
server 2019-08-10T19:44:11.450Z [Read cache] error: Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.
这是我的代码:
// Fetch a potential query result for the given query, if it exists in the cache already
let cachedItem;
try {
cachedItem = await redisClient.get(queryString); // This emit an error on the redis client, because it fails to connect (that's intended, to test the behaviour)
} catch (e) {
logger.error(e); // It never goes there, as the error isn't "thrown", but rather "emitted" and handled by redis its own way
epsagon.setError(e);
}
// If the query is cached, return the results from the cache
if (cachedItem) {
// return item
} else {} // fetch from another endpoint (fallback backup)
我的理解是redis的错误是通过client.emit('error', error)
处理的,这是异步的,被调用者不会抛出错误,这不允许调用者使用try/catch来处理错误。
是否应该以一种非常特殊的方式处理 Redis 错误?难道不能像我们通常处理大多数错误那样捕获它们吗?
此外,在抛出致命异常(进程停止)之前,redis 似乎重试了 20 次(默认情况下)连接。但我想处理任何异常并以我自己的方式处理它。
我已经通过提供错误的连接数据测试了 redis 客户端的行为,这导致无法连接,因为那里没有可用的 redis 实例 url,我的目标是最终捕获所有类型的 redis错误并优雅地处理它们。
客户端 Redis
对象上的连接错误 are reported as an error
event。
根据 "Auto-reconnect" section of the docs,ioredis 将在与 Redis 的连接丢失时自动尝试重新连接(或者,大概无法首先建立连接)。只有在 maxRetriesPerRequest
次尝试后,未决命令才会 "be flushed with an error",即到达 catch
此处:
try {
cachedItem = await redisClient.get(queryString); // This emit an error on the redis client, because it fails to connect (that's intended, to test the behaviour)
} catch (e) {
logger.error(e); // It never goes there, as the error isn't "thrown", but rather "emitted" and handled by redis its own way
epsagon.setError(e);
}
由于您在遇到第一个错误时停止程序:
client.on('error', function (e) {
// ...
if (e.message === 'ERR invalid password') {
logger.error(`Fatal error occurred "${e.message}". Stopping server.`);
throw e; // Fatal error, don't attempt to fix
...重试和随后的 "flushing with an error" 永远没有机会 运行。
忽略 client.on('error'
中的错误,您应该得到 await redisClient.get()
返回的错误。
以下是我的团队在 TypeScript 项目中使用 IORedis 完成的工作:
let redis;
const redisConfig: Redis.RedisOptions = {
port: parseInt(process.env.REDIS_PORT, 10),
host: process.env.REDIS_HOST,
autoResubscribe: false,
lazyConnect: true,
maxRetriesPerRequest: 0, // <-- this seems to prevent retries and allow for try/catch
};
try {
redis = new Redis(redisConfig);
const infoString = await redis.info();
console.log(infoString)
} catch (err) {
console.log(chalk.red('Redis Connection Failure '.padEnd(80, 'X')));
console.log(err);
console.log(chalk.red(' Redis Connection Failure'.padStart(80, 'X')));
// do nothing
} finally {
await redis.disconnect();
}
我正在尝试优雅地处理 Redis 错误,以便绕过错误并执行其他操作,而不是让我的应用程序崩溃。
但到目前为止,我不能只捕获 ioredis 抛出的异常,它绕过了我的 try/catch
并终止了当前进程。当前的行为不允许我优雅地处理错误,也不允许我从替代系统(而不是 redis)获取数据。
import { createLogger } from '@unly/utils-simple-logger';
import Redis from 'ioredis';
import epsagon from './epsagon';
const logger = createLogger({
label: 'Redis client',
});
/**
* Creates a redis client
*
* @param url Url of the redis client, must contain the port number and be of the form "localhost:6379"
* @param password Password of the redis client
* @param maxRetriesPerRequest By default, all pending commands will be flushed with an error every 20 retry attempts.
* That makes sure commands won't wait forever when the connection is down.
* Set to null to disable this behavior, and every command will wait forever until the connection is alive again.
* @return {Redis}
*/
export const getClient = (url = process.env.REDIS_URL, password = process.env.REDIS_PASSWORD, maxRetriesPerRequest = 20) => {
const client = new Redis(`redis://${url}`, {
password,
showFriendlyErrorStack: true, // See https://github.com/luin/ioredis#error-handling
lazyConnect: true, // XXX Don't attempt to connect when initializing the client, in order to properly handle connection failure on a use-case basis
maxRetriesPerRequest,
});
client.on('connect', function () {
logger.info('Connected to redis instance');
});
client.on('ready', function () {
logger.info('Redis instance is ready (data loaded from disk)');
});
// Handles redis connection temporarily going down without app crashing
// If an error is handled here, then redis will attempt to retry the request based on maxRetriesPerRequest
client.on('error', function (e) {
logger.error(`Error connecting to redis: "${e}"`);
epsagon.setError(e);
if (e.message === 'ERR invalid password') {
logger.error(`Fatal error occurred "${e.message}". Stopping server.`);
throw e; // Fatal error, don't attempt to fix
}
});
return client;
};
我正在模拟一个错误的 password/url 以查看配置错误时 redis 的反应。我已将 lazyConnect
设置为 true
以处理调用者的错误。
但是,当我将 url 定义为 localhoste:6379
(而不是 localhost:6379
) 时,出现以下错误:
server 2019-08-10T19:44:00.926Z [Redis client] error: Error connecting to redis: "Error: getaddrinfo ENOTFOUND localhoste localhoste:6379"
(x 20)
server 2019-08-10T19:44:11.450Z [Read cache] error: Reached the max retries per request limit (which is 20). Refer to "maxRetriesPerRequest" option for details.
这是我的代码:
// Fetch a potential query result for the given query, if it exists in the cache already
let cachedItem;
try {
cachedItem = await redisClient.get(queryString); // This emit an error on the redis client, because it fails to connect (that's intended, to test the behaviour)
} catch (e) {
logger.error(e); // It never goes there, as the error isn't "thrown", but rather "emitted" and handled by redis its own way
epsagon.setError(e);
}
// If the query is cached, return the results from the cache
if (cachedItem) {
// return item
} else {} // fetch from another endpoint (fallback backup)
我的理解是redis的错误是通过client.emit('error', error)
处理的,这是异步的,被调用者不会抛出错误,这不允许调用者使用try/catch来处理错误。
是否应该以一种非常特殊的方式处理 Redis 错误?难道不能像我们通常处理大多数错误那样捕获它们吗?
此外,在抛出致命异常(进程停止)之前,redis 似乎重试了 20 次(默认情况下)连接。但我想处理任何异常并以我自己的方式处理它。
我已经通过提供错误的连接数据测试了 redis 客户端的行为,这导致无法连接,因为那里没有可用的 redis 实例 url,我的目标是最终捕获所有类型的 redis错误并优雅地处理它们。
客户端 Redis
对象上的连接错误 are reported as an error
event。
根据 "Auto-reconnect" section of the docs,ioredis 将在与 Redis 的连接丢失时自动尝试重新连接(或者,大概无法首先建立连接)。只有在 maxRetriesPerRequest
次尝试后,未决命令才会 "be flushed with an error",即到达 catch
此处:
try {
cachedItem = await redisClient.get(queryString); // This emit an error on the redis client, because it fails to connect (that's intended, to test the behaviour)
} catch (e) {
logger.error(e); // It never goes there, as the error isn't "thrown", but rather "emitted" and handled by redis its own way
epsagon.setError(e);
}
由于您在遇到第一个错误时停止程序:
client.on('error', function (e) {
// ...
if (e.message === 'ERR invalid password') {
logger.error(`Fatal error occurred "${e.message}". Stopping server.`);
throw e; // Fatal error, don't attempt to fix
...重试和随后的 "flushing with an error" 永远没有机会 运行。
忽略 client.on('error'
中的错误,您应该得到 await redisClient.get()
返回的错误。
以下是我的团队在 TypeScript 项目中使用 IORedis 完成的工作:
let redis;
const redisConfig: Redis.RedisOptions = {
port: parseInt(process.env.REDIS_PORT, 10),
host: process.env.REDIS_HOST,
autoResubscribe: false,
lazyConnect: true,
maxRetriesPerRequest: 0, // <-- this seems to prevent retries and allow for try/catch
};
try {
redis = new Redis(redisConfig);
const infoString = await redis.info();
console.log(infoString)
} catch (err) {
console.log(chalk.red('Redis Connection Failure '.padEnd(80, 'X')));
console.log(err);
console.log(chalk.red(' Redis Connection Failure'.padStart(80, 'X')));
// do nothing
} finally {
await redis.disconnect();
}