Documentation ¶
Overview ¶
Package dnscache provides a thread-safe local single-flight DNS cache for LookupHost. The cache has a maximum size and a time-to-live (TTL) for each DNS entry. Duplicate LookupHost calls for the same host will wait for the first lookup to complete and return the same value.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶ added in v1.83.0
type Cache struct {
// contains filtered or unexported fields
}
Cache represents the single-flight DNS cache.
func New ¶
New creates a new single-flight DNS cache of the specified size and TTL. If the resolver parameter is nil, a default net.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 (*Cache) DialContext ¶ added in v1.83.0
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 (*Cache) LookupHost ¶ added in v1.83.0
LookupHost performs a DNS lookup for the given host. Duplicate lookup calls for the same host will wait for the first lookup to complete (single-flight). It also handles the case where the cache entry is removed or updated during the wait. The function returns the cached value if available; otherwise, it performs a new lookup. If the external lookup call is successful, it updates the cache with the newly obtained value.