dnscache

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: MIT Imports: 6 Imported by: 3

README

go-dnscache Go Documentation CircleCI (all branches)

go-dnscache is a Go package for caching DNS lookup results in memory. It asynchronously lookups DNS and refresh 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.

Install

Use go get:

$ go get -u go.mercari.io/go-dnscache

Usage

All usage are described in GoDoc.

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

func WithLogger(logger *slog.Logger) Option

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, options ...Option) (*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