javascript - 为什么启用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 服务。
解决方案
我们有一个涵盖此主题的完整文档: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]
您还需要查看客户端可用的任何重试方法。我希望这有帮助。
推荐阅读
- r - 重复六边形多边形的过程,为大量多边形定义光栅边界
- python - 在 Django Rest Framework 中发布外键 ID
- python - aiohttp - 在请求每个 API 调用之前
- c++ - 如何在自定义 Tensorflow Op Kernel 中生成随机张量
- java - 如何打印对象的名称?
- asp.net - 在 IIS 上运行的 ASP.NET Web API 2 应用程序向 IIS express 上的本地应用程序返回 OK 数据,但在将请求应用程序部署到 IIS 后未经授权
- javascript - React Router Link 显示两个组件
- angular - Angular 8:输入/输出与服务
- python - 将多个相同产品添加到购物车时无法增加产品数量
- computer-vision - 估计主动光立体系统的深度误差?