Documentation ¶
Overview ¶
Package dnscache is a Go package for caching DNS lookup results in memory. It asynchronously lookups DNS and refreshes results. The main motivation of this package is to avoid too much DNS lookups for every request (DNS lookup sometimes makes request really slow and causes error). This can be mainly used for the targets which are running on non-dynamic environment where IP does not change often.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DialFunc ¶
func DialFunc(resolver *Resolver, baseDialFunc dialFunc) dialFunc
DialFunc is a helper function which returns `net.DialContext` function. It randomly fetches an IP from the DNS cache and dials it by the given dial function. It dials one by one and returns first connected `net.Conn`. If it fails to dial all IPs from cache it returns first error. If no baseDialFunc is given, it sets default dial function.
You can use returned dial function for `http.Transport.DialContext`.
In this function, it uses functions from `rand` package. To make it really random, you MUST call `rand.Seed` and change the value from the default in your application
Example ¶
logger := slog.Default().WithGroup("dnscache") resolver, _ := New(3*time.Second, 5*time.Second, WithLogger(logger)) // You can create a HTTP client which selects an IP from dnscache // randomly and dials it. rand.Seed(time.Now().UTC().UnixNano()) // You MUST run in once in your application client := http.Client{ Transport: &http.Transport{ DialContext: DialFunc(resolver, nil), }, } // Do what you want. _ = client
Output:
Types ¶
type Option ¶ added in v0.3.0
type Option struct {
// contains filtered or unexported fields
}
func WithLogger ¶ added in v0.3.0
type Resolver ¶
type Resolver struct {
// contains filtered or unexported fields
}
Resolver is DNS cache resolver which cache DNS resolve results in memory.
func New ¶
New initializes DNS cache resolver and starts auto refreshing in a new goroutine. To stop refreshing, call `Stop()` function.
func (*Resolver) Fetch ¶
Fetch fetches IP list from the cache. If IP list of the given addr is not in the cache, then it lookups from DNS server by `Lookup` function.