yuyuyu
V2EX  ›  Node.js

nodejs dns.lookup 很慢很慢

  •  
  •   yuyuyu · Dec 6, 2016 · 3127 views
    This topic created in 3465 days ago, the information mentioned may be changed or developed.
    var http = require('http');
    var url = require('url');
    var dns = require('dns');
    var qs = require('querystring');
    
    dns.setServers(['223.5.5.5']);
    var server = http.createServer(function (req, res) {
        var uri = url.parse(req.url);
        var h = qs.parse(uri.query).host;
        if (!h) {
    	res.end();
            return;
        }
        if (uri.pathname === '/dnsQuery') {
            //console.log(h);
            dns.lookup(h, { family: 4, all: true }, function (err, addrs) {
                if (err) {
                    console.error(h + ":" + err);
                    res.end();
                }
                else {
                    var result=addrs.map(r=>r.address).join(',');
                    console.log(h+':'+result);
                    res.setHeader('Cache-Control', 'public, max-age=3600');
                    res.end(result);
                }
    	});
        }
    });
    server.listen(8080);
    

    很简单的一段代码,一开始还好,在并发量(也不多估计才几十到一百每秒)上来以后就查询的非常慢,前面挂的 nginx 60 秒后就 504 了,而且当我查询已经查询过的域名的时候也是 504 ,好像 nginx 的缓存没有生效一样,用 pm2 list 确认了 app 还在运行,并且 pm2 logs 里也有查询的记录

    4 replies    2016-12-10 09:15:22 +08:00
    zbinlin
        1
    zbinlin  
       Dec 7, 2016
    你可以看下官方文档里关于 dns.lookup 的说明:
    https://nodejs.org/dist/latest-v6.x/docs/api/dns.html#dns_dns_lookup
    jacobbubu
        2
    jacobbubu  
       Dec 7, 2016
    dns.lookup 和 nginx 无关。 node.js 的 dns.lookup 实现是无缓存的,如果需要缓存自己来做。
    mcree
        3
    mcree  
       Dec 10, 2016
    不一定是这个原因,不过上游 DNS 服务器可能有 rate-limit 。
    yunshansimon
        4
    yunshansimon  
       Dec 10, 2016
    使用 dns.resolve()代替 lookup()再试试
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3386 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 11:12 · PVG 19:12 · LAX 04:12 · JFK 07:12
    ♥ Do have faith in what you're doing.