因为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)都会被阻塞(等待数据返回),这一点要注意。