为什么Node.js中部分TCP套接字连接耗时远高于其他连接?
我开发了一个小型库tcp-exists,用于检测TCP端点是否存在,支持遍历网络查找特定端口上的服务,库代码已开源至GitHub。
遍历扫描过程中发现一个异常问题:tcp-exists创建的部分TCP套接字在连接100%可正常访问的端点时耗时极长,正常情况下仅需5-20ms即可完成的连接,异常时耗时可达1-3s。
复现代码如下:
import tcpExists from 'tcp-exists' async function main () { for (let i = 1; i < 250; ++i) { let time = performance.now() const exists = await tcpExists('192.168.1.87', 80, 2000) time = performance.now() - time console.log(`time [${i}]:`, time) if (time > 500) console.log('=====STRANGE=====') if (!exists) console.log('exists') } } main()
这个问题的出现没有固定规律,无论是检测同一IP的相同端口、同一IP的不同端口,还是不同IP的端口,都会出现部分连接耗时为正常情况100倍的问题。该问题会导致明明端点存在,却因超时设置过短让函数错误返回false。
但我无法将单次检测的超时设置超过25ms:我的使用场景需要扫描192.168.0.0到192.168.255.255网段共65536个端点,其中绝大多数IP没有对应服务,需要靠超时返回false,如果设置1s超时,总扫描耗时将达65536秒,约18小时,完全无法接受。
内容的提问来源于stack exchange,提问作者JerryCauser
相关产品推荐
相关产品推荐

