client

package
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Jan 24, 2024 License: MIT Imports: 12 Imported by: 1

Documentation

Overview

Package client implements DNS client wrappers

Index

Constants

View Source
const (
	// DefaultSingleFlightExpiration tells how long will we cache
	// the result after an exchange
	DefaultSingleFlightExpiration = 1 * time.Second
)

Variables

View Source
var (
	// DefaultWorkerPoolSize indicates how many parallel workers a
	// [WorkerPool] contains if the amount wasn't specified when created.
	DefaultWorkerPoolSize = runtime.NumCPU()
)

Functions

func HasIPv6Support added in v0.7.8

func HasIPv6Support() bool

HasIPv6Support tells if the system supports IPv6 or not. This doesn't guarantee connections will be successful.

func NewDefaultClient

func NewDefaultClient(udpSize uint16) *dns.Client

NewDefaultClient allocate a default dns.Client in the same manner as dns.ExchangeContext(), plain UDP.

func Unwrap added in v0.7.6

func Unwrap(c Client) *dns.Client

Unwrap uses the Unwrapper interface to find the underlying *dns.Client

Types

type Auto added in v0.7.7

type Auto struct {
	UDP Client
	TCP Client
	TLS Client
	// contains filtered or unexported fields
}

Auto is a client that allows different networks based on the server's prefix. * udp:// for UDP-only * tcp:// for TCP-only * tls:// for TCP+TLS * and without prefix for TCP-fallback

func NewAutoClient added in v0.7.7

func NewAutoClient(udp, tcp Client, exp time.Duration) (*Auto, error)

NewAutoClient allocates a new Auto client. If changes to fields are done manually after this call, or manually assembling the Auto struct, it is required to call Auto.SetDefaults.

NewAutoClient allows specifying a custom expiration value for SingleFlight, but when Auto is assembled manually or `exp == 0`, DefaultSingleFlightExpiration will be used.

func (*Auto) ExchangeContext added in v0.7.7

func (c *Auto) ExchangeContext(ctx context.Context, req *dns.Msg,
	server string) (*dns.Msg, time.Duration, error)

ExchangeContext uses different exchange networks based on the prefix of the server string.

func (*Auto) SetDefaults added in v0.7.7

func (c *Auto) SetDefaults() error

SetDefaults fills the configuration gaps

type Client

type Client interface {
	ExchangeContext(context.Context, *dns.Msg, string) (*dns.Msg, time.Duration, error)
}

A Client makes a request to a server

type ExchangeFunc added in v0.7.6

type ExchangeFunc func(context.Context, *dns.Msg, string) (*dns.Msg, time.Duration, error)

ExchangeFunc is a function that implements the Client interface

func (ExchangeFunc) ExchangeContext added in v0.7.6

func (fn ExchangeFunc) ExchangeContext(ctx context.Context, req *dns.Msg,
	server string) (*dns.Msg, time.Duration, error)

ExchangeContext implements the Client interface

type NoAAAA added in v0.7.10

type NoAAAA struct {
	Client
}

NoAAAA is a dns.Client middleware to remove AAAA entries from all responses

func NewNoAAAA added in v0.7.10

func NewNoAAAA(c Client) *NoAAAA

NewNoAAAA creates a Client middleware that filters out all AAAA entries

func (NoAAAA) ExchangeContext added in v0.7.10

func (c NoAAAA) ExchangeContext(ctx context.Context, req *dns.Msg,
	server string) (*dns.Msg, time.Duration, error)

ExchangeContext calls the next client in the chain if it's not an AAAA, and discards all AAAA entries on the response.

func (NoAAAA) Unwrap added in v0.7.10

func (c NoAAAA) Unwrap() *dns.Client

type SingleFlight

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

SingleFlight wraps a Client to minimize redundant queries

func NewSingleFlight

func NewSingleFlight(c Client, exp time.Duration) *SingleFlight

NewSingleFlight creates a SingleFlight Client around another. if no Client is specified, the default udp dns.Client will be used. if exp is positive, the result will be cached that long. if exp is negative, the result will expire immediately if exp is zero, DefaultSingleFlightExpiration will be used

func (*SingleFlight) ExchangeContext

func (sfc *SingleFlight) ExchangeContext(ctx context.Context, req *dns.Msg,
	server string) (*dns.Msg, time.Duration, error)

ExchangeContext makes a DNS query to a server, minimizing duplications.

func (*SingleFlight) RequestKey

func (*SingleFlight) RequestKey(req *dns.Msg, server string) string

RequestKey serializes a DNS request to act as temporary cache key

func (*SingleFlight) Unwrap added in v0.7.6

func (sfc *SingleFlight) Unwrap() *dns.Client

Unwrap returns the underlying *dns.Client

type Unwrapper added in v0.7.6

type Unwrapper interface {
	Unwrap() *dns.Client
}

An Unwrapper can tell what's the underlying *dns.Client

type Worker added in v0.8.1

type Worker interface {
	Client

	// Start starts the workers connected to the given
	// context.
	Start(context.Context) error
	// Shutdown initiates a shut down and wait until
	// all workers have finished or the provided context
	// has been canceled or expired.
	Shutdown(context.Context) error
	// Cancel initiates a shut down with a reason, and
	// indicates if it was the first cancellation request
	// or not.
	Cancel(error) bool
}

A Worker is a Client that needs to be started/shutdown.

type WorkerPool added in v0.8.1

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

A WorkerPool limits the number of parallel requests.

func NewWorkerPool added in v0.8.1

func NewWorkerPool(c Client, maxWorkers int) (*WorkerPool, error)

NewWorkerPool creates a WorkerPool with a specified number of workers using the given Client.

func (*WorkerPool) Cancel added in v0.8.1

func (wp *WorkerPool) Cancel(reason error) bool

Cancel initiates a shutdown.

func (*WorkerPool) Cancelled added in v0.8.1

func (wp *WorkerPool) Cancelled() <-chan struct{}

Cancelled returns a channel that indicates when a shutdown has started/

func (*WorkerPool) Done added in v0.8.1

func (wp *WorkerPool) Done() <-chan struct{}

Done returns a channel that indicates when all workers have finished.

func (*WorkerPool) ExchangeContext added in v0.8.1

func (wp *WorkerPool) ExchangeContext(ctx context.Context,
	req *dns.Msg, server string) (*dns.Msg, time.Duration, error)

ExchangeContext implements a restricted parallel Client interface.

func (*WorkerPool) IsCancelled added in v0.8.1

func (wp *WorkerPool) IsCancelled() bool

IsCancelled tells if shutdown has been initiated

func (*WorkerPool) OnShutdown added in v0.8.1

func (wp *WorkerPool) OnShutdown(fn func(error))

OnShutdown receives a function to call when shutdown has been initiated, and the cause.

func (*WorkerPool) Shutdown added in v0.8.1

func (wp *WorkerPool) Shutdown(ctx context.Context) error

Shutdown initiates a shutdown and waits until all workers have finished or the given context expires.

func (*WorkerPool) Start added in v0.8.1

func (wp *WorkerPool) Start(ctx context.Context) error

Start launches the workers

func (*WorkerPool) Unwrap added in v0.8.1

func (wp *WorkerPool) Unwrap() *dns.Client

Unwrap returns the underlying dns.Client

func (*WorkerPool) Wait added in v0.8.1

func (wp *WorkerPool) Wait() error

Wait blocks until all workers have finished.

Jump to

Keyboard shortcuts

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