首页 > 解决方案 > 为什么启用keep-alive时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 客户端

有谁知道这是什么原因?按照这里的建议https://github.com/luin/ioredis/blob/master/API.md已经默认启用了 keep-alive

如果发生超时,我希望客户端永远不会超时并重新连接。我正在使用 Azure 的 Redis 服务。

标签: javascriptnode.jsredisazure-redis-cacheioredis

解决方案


我们有一个涵盖此主题的完整文档: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]

您还需要查看客户端可用的任何重试方法。我希望这有帮助。


推荐阅读