Node.js当中的ioredis设置timeout的问题

因为node.js内部使用libuv实现网络io调用(使用epoll),在socket层面是不能设置read timeout,connection timeout,write timeout。

所以node.js api中net模块只暴露了socket.setTimeout ,用于在tcp通信空闲时间超过设置的值,所触发的事件;而ioredis在connectTimeout只在初始化socket连接时启用,连接成功后空闲超时是没有启用的。

可以通过https://github.com/luin/ioredis/blob/master/lib/redis.js#L301 查看:

if (options.connectTimeout) {
                /*
                 * Typically, Socket#setTimeout(0) will clear the timer
                 * set before. However, in some platforms (Electron 3.x~4.x),
                 * the timer will not be cleared. So we introduce a variable here.
                 *
                 * See https://github.com/electron/electron/issues/14915
                 */
                var connectTimeoutCleared = false;
                stream.setTimeout(options.connectTimeout, function () {
                    if (connectTimeoutCleared) {
                        return;
                    }
                    stream.setTimeout(0);
                    stream.destroy();
                    var err = new Error('connect ETIMEDOUT');
                    err.errorno = 'ETIMEDOUT';
                    err.code = 'ETIMEDOUT';
                    err.syscall = 'connect';
                    eventHandler.errorHandler(_this)(err);
                });
                /* 只要触发一次 CONNECT_EVENT(connect) 事件setTimeout空闲过期时间就会改为0,即禁用空闲超时  */
                stream.once(CONNECT_EVENT, function () {
                    connectTimeoutCleared = true;
                    stream.setTimeout(0);
                });
            }

ioredis没有实现连接池,但使用了node.js内部的epoll,并且ioredis实现了pipeline方式,所以性能上应该不会太差,但如果发送的命令阻塞redis或者使用wait这种命令(阻塞session级别)会使只要调用redis的router(await redis)都会被阻塞(等待数据返回),这一点要注意。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值