read timeout,connection timeout,write timeout
Socket hang up
服务端挂断链接
Error: ETIMEDOUT
at Timeout._onTimeout (/Users/test/node_modules/request/request.js:677:15)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) {
code: 'ETIMEDOUT'
}
ETIMEDOUT
客户端定义了超时时间,主动报错
const request = require('request');
request({
url: 'http://127.0.0.1:3020/timeout',
timeout: 500,
}, (err, response, body) => {
console.log(err, body);
});
Error: ETIMEDOUT
at Timeout._onTimeout (/Users/test/node_modules/request/request.js:677:15)
at listOnTimeout (internal/timers.js:531:17)
at processTimers (internal/timers.js:475:7) {
code: 'ETIMEDOUT'
}
ESOCKETTIMEDOUT
默认情况下,Node开辟四个workers执行用户tasks包括解析DNS查询,如果DNS查询较慢,就会阻塞请求在DNS阶段,最终超时返回 ESOCKETTIMEDOUT 或者 ETIMEDOUT 错误码,而由于workers数量有限,又被占用时其他任务只能排队,最终导致越来越多的超时错误。
而worker thread默认数量4也是由Libuv定义的,但是它可以通过定义 UV_THREADPOOL_SIZE 环境变量来修改默认值,最大值为128。对于我们的问题,为了应对频繁请求(包含可能较慢的DNS解析任务),需要增大线程池的容量,比如将默认4 worker threads提高至10。
解决方案:
调整nodejs 线程池
重试机制
通过(redis key, transition_id)实现接口幂等性
参考文档
https://blog.csdn.net/qq_15437667/article/details/106238670
浅淡 ETIMEDOUT & ESOCKETTIMEDOUT & Socket hang up
微服务调用超时处理
分布式框架服务调用超时设计与实现