2

In the "net/http" package I can cache the DNS lookups by:

client := &http.Client{
        Transport: &http.Transport{
            Dial: (&nett.Dialer{
                Resolver: &nett.CacheResolver{TTL: 5 * time.Minute},
                IPFilter: nett.DualStack,
            }).Dial,
        },

    }

then use client to retrieve websites. How do I cache the DNS lookups for the net package? for instance, a reverse DNS request:

net.LookupAddr(ip)

Since this does not use a variable I am confused as to how to get it setup and how to even know if I am using a cached instance.

user_78361084
  • 3,988
  • 18
  • 68
  • 130
  • 1
    DNS caching is typically handled by your DNS server, not the client. – Flimzy Jan 25 '19 at 07:59
  • 1
    Possible duplicate of [Does Go cache DNS lookups?](https://stackoverflow.com/questions/40251727/does-go-cache-dns-lookups) – Flimzy Jan 25 '19 at 08:01

2 Answers2

2

The nett package seems to just have a single "Resolve" method rather than LookupAddr, LookupIP etc etc that the official net package has. So reverse lookups don't seem to be available. Here's how to do a normal lookup of address from name

package main

import (
    "github.com/abursavich/nett"
    "time"
)

func main() {
    r := nett.CacheResolver{TTL: 5 * time.Minute}
    a, _ := r.Resolve("muppet.com")
    for _, i := range a {
        print(i.String())
    }
}
Vorsprung
  • 28,957
  • 4
  • 32
  • 55
1

It would seem the behavior of the net.LookupAddr is to use the host resolver. The way I do this for my services in prod is to run dnsmasq on the hosts so the DNS lookups are cached per host. The docs do mention you can customize the behavior by implementing a custom Resolver as you did: https://golang.org/pkg/net/#LookupAddr

But I think the piece you're looking for is (from the top of that doc page):

DefaultResolver is the resolver used by the package-level Lookup functions and by Dialers without a specified Resolver.

danehammer
  • 386
  • 3
  • 13