当我的脚本闲置一段时间后,我收到了以下错误。我不明白这是为什么。
error: [ioredis] Unhandled error event:
error: Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
error: [ioredis] Unhandled error event
error: Error: read ETIMEDOUT
at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
我将我的 redis 客户端初始化为:
let redis = require("ioredis");
redis = Promise.promisifyAll(redis);
const redis = new redis({
host: "my hostname",
port: 6379,
password: "some password"
});
我正在使用 ioredis client .
有人知道这是什么原因吗?按照此处的建议,默认情况下已启用保持事件状态 https://github.com/luin/ioredis/blob/master/API.md
我希望客户端永远不会超时并在发生超时时重新连接。我正在使用 azure 的 Redis 服务。
最佳答案
我们有一个涵盖该主题的完整文档:Troubleshoot Azure Cache for Redis timeouts
如果使用 StackExchange.Redis 客户端,建议使用以下模式的最佳实践:
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
return ConnectionMultiplexer.Connect("cachename.redis.cache.windows.net,abortConnect=false,ssl=true,password=...");
});
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
对于ioredis,你可以设置一个客户端属性:[options.lazyConnect]
您还需要查看您的客户端可用的任何重试方法。我希望这会有所帮助。
关于javascript - 为什么ioredis客户端开启keep-alive会超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60924392/