javascript - 为什么ioredis客户端开启keep-alive会超时?

标签 javascript node.js redis azure-redis-cache ioredis

当我的脚本闲置一段时间后,我收到了以下错误。我不明白这是为什么。

    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/

相关文章:

javascript - 仅将函数应用于父级

javascript - 如何在 Node.js 上使用 JS 库?

redis - 如何知道jedis消息有没有推送

php - ZF2 Redis 适配器 : getting TTL or setting a new expiration for a key?

ruby-on-rails - Redis服务器重启时如何自动重启Resque worker

JavaScript 语法 : inserting the value of a string into a statement

javascript - 在 Node 中异步执行 CPU 密集型 HTTP 请求

node.js - sequelize - 如何为日期字段设置验证规则

node.js - 使用特定配置打开 Puppeteer(下载 PDF 而不是 PDF 查看器)

JavaScript 将变量声明为 "g = g || {}; "这意味着什么?