dnscache

package
v1.82.0 Latest Latest
Warning

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

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

Documentation

Overview

Package dnscache provides a local DNS cache for LookupHost. The cache has a maximum size and a time-to-live (TTL) for each DNS entry.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheResolver

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

CacheResolver represents a cache for DNS items.

func New

func New(resolver Resolver, size int, ttl time.Duration) *CacheResolver

New creates a new DNS resolver with a cache of the specified size and TTL. If the resolver parameter is nil, a default resolver will be used. The size parameter determines the maximum number of DNS entries that can be cached (min = 1). If the size is less than or equal to zero, the cache will have a default size of 1. The ttl parameter specifies the time-to-live for each cached DNS entry.

func (*CacheResolver) DialContext

func (r *CacheResolver) DialContext(ctx context.Context, network, address string) (net.Conn, error)

DialContext dials the network and address specified by the parameters. It resolves the host from the address using the LookupHost method of the Resolver. It then attempts to establish a connection to each resolved IP address until a successful connection is made. If all connection attempts fail, it returns an error. The function returns the established net.Conn and any error encountered during the process. This function can replace the DialContext in http.Transport.

func (*CacheResolver) LookupHost

func (r *CacheResolver) LookupHost(ctx context.Context, host string) ([]string, error)

LookupHost performs a DNS lookup for the given host using the DNSCacheResolver. It first checks if the host is already cached and not expired. If so, it returns the cached addresses. Otherwise, it performs a DNS lookup using the underlying Resolver and caches the obtained addresses for future use.

func (*CacheResolver) RemoveEntry

func (r *CacheResolver) RemoveEntry(host string)

RemoveEntry removes the cache entry for the specified host.

func (*CacheResolver) Reset

func (r *CacheResolver) Reset()

Reset clears the whole cache and initializes it with a new map of the specified size.

type Resolver

type Resolver interface {
	LookupHost(ctx context.Context, host string) (addrs []string, err error)
}

Resolver is a net.Resolver interface for DNS lookups.

Jump to

Keyboard shortcuts

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