Go 缓存 DNS 查找吗? [英] Does Go cache DNS lookups?

查看:28
本文介绍了Go 缓存 DNS 查找吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个测试爬虫,想知道 Go (golang) 是否缓存 DNS 查询.我在 dnsclient 中没有看到任何关于缓存的内容.这似乎是添加到任何爬虫以防止大量额外 DNS 查询的重要内容.

I am building a test crawler and wanted to know if Go (golang) caches DNS queries. I don't see anything about caching in the dnsclient. This seems like an important thing to add to any crawler to prevent lots of extra DNS queries.

Go (1.4+) 是否缓存 DNS 查找?

如果没有,debian/ubuntu/linux、windows 或 darwin/OSX 是否会在网络级别进行缓存,Go 会从中受益吗?

If not, does debian/ubuntu/linux, windows, or darwin/OSX do any caching at the network level Go benefits from?

推荐答案

您的问题的答案是否定的.std lib 解析器中没有内置的 dns 缓存.会有帮助吗?也许在某些情况下.我们的组织在每台服务器上运行本地 dns 缓存并将 resolv.conf 指向那里.因此,在语言中进行缓存不一定对我们有很大帮助.

The answer to your question is no. There is no built-in dns caching in the std lib resolver. Would it be helpful? Maybe in some cases. Our org runs a local dns cache on each server and points resolv.conf there. So it wouldn't necessarily help us much to have caching in the language.

有一些解决方案可以帮助您.这个包 似乎有一个很好的解决方案.从他们自述文件中的片段中,您甚至可以执行以下操作:

There are some solutions that could help you. This package seems to have a pretty good solution. From the snippet in their readme you could even do:

http.DefaultClient.Transport = &http.Transport {
  MaxIdleConnsPerHost: 64,
  Dial: func(network string, address string) (net.Conn, error) {
    separator := strings.LastIndex(address, ":")
    ip, _ := dnscache.FetchString(address[:separator])
    return net.Dial("tcp", ip + address[separator:])
  },
}

为来自 http.Get 和朋友的所有 http 请求启用它.

To enable it for all http requests from http.Get and friends.

这篇关于Go 缓存 DNS 查找吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆