10

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.

Does Go (1.4+) cache DNS lookups?

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

DaSourcerer
  • 5,318
  • 4
  • 26
  • 52
Xeoncross
  • 50,836
  • 73
  • 238
  • 351
  • No, but it's quite common to use a local dns server to cache responses. – JimB Oct 26 '16 at 13:14
  • 1
    You don't want Go to do this. Go the runtime / stdlib / language absolutely should not, and your app probably shouldn't either. Java does this and it's a pain and leads to all sorts of unexpected situations. How do you clear the cache without modifying or restarting the program? How do you know what's in the cache? How long is it there for? How do you troubleshoot it? It's opaque. – cbednarski May 05 '19 at 04:37

2 Answers2

12

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:])
  },
}

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

captncraig
  • 19,430
  • 11
  • 95
  • 139
  • 2
    In addition, many Linuxes now include local DNS caching so there is not much benefit to be had from extra caching within apps. Ubuntu is the obvious example. You can see this in action by e.g. `dig www.google.com`, which shows some comments that might include `;; SERVER: 127.0.1.1`, this being your local DNS cache in operation. – Rick-777 Nov 04 '16 at 22:26
  • It's better to use `net.SplitHostPort` to split `address`. – Zhang Boyang May 19 '20 at 04:37
4

The Go resolver does not do any in-process caching. While it would be possible to roll your own, your best bet is probably to run a system-wide DNS cache on each machine. (My favourite being dnsmasq.)

jch
  • 4,491
  • 16
  • 36