dnscache

package
v0.4.15 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2024 License: MIT, MIT Imports: 7 Imported by: 0

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.

Code was adopted from https://github.com/mercari/go-dnscache

Index

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

Types

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver is DNS cache resolver which cache DNS resolve results in memory.

func New

func New(freq time.Duration, lookupTimeout time.Duration, logger *logrus.Logger) (*Resolver, error)

New initializes DNS cache resolver and starts auto refreshing in a new goroutine. To stop refreshing, call `Stop()` function.

func (*Resolver) Fetch

func (r *Resolver) Fetch(ctx context.Context, addr string) ([]net.IP, error)

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.

func (*Resolver) LookupIP

func (r *Resolver) LookupIP(ctx context.Context, addr string) ([]net.IP, error)

LookupIP lookups IP list from DNS server then it saves result in the cache. If you want to get result from the cache use `Fetch` function.

func (*Resolver) Refresh

func (r *Resolver) Refresh()

Refresh refreshes IP list cache.

func (*Resolver) Stop

func (r *Resolver) Stop()

Stop stops auto refreshing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL