refactor(netx): move construction logic outside package (#798)
For testability, replace most if-based construction logic with calls to well-tested factories living in other packages. While there, acknowledge that a bunch of types could now be private and make them private, modifying the code to call the public factories allowing to construct said types instead. Part of https://github.com/ooni/probe/issues/2121
This commit is contained in:
parent
2d3d5d9cdc
commit
6b85dfce88
20 changed files with 475 additions and 184 deletions
|
|
@ -2,44 +2,90 @@ package netx
|
|||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/ooni/probe-cli/v3/internal/model"
|
||||
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
||||
)
|
||||
|
||||
// CacheResolver is a resolver that caches successful replies.
|
||||
type CacheResolver struct {
|
||||
ReadOnly bool
|
||||
model.Resolver
|
||||
mu sync.Mutex
|
||||
cache map[string][]string
|
||||
// MaybeWrapWithCachingResolver wraps the provided resolver with a resolver
|
||||
// that remembers the result of previous successful resolutions, if the enabled
|
||||
// argument is true. Otherwise, we return the unmodified provided resolver.
|
||||
//
|
||||
// Bug: the returned resolver only applies caching to LookupHost and any other
|
||||
// lookup operation returns ErrNoDNSTransport to the caller.
|
||||
func MaybeWrapWithCachingResolver(enabled bool, reso model.Resolver) model.Resolver {
|
||||
if enabled {
|
||||
reso = &cacheResolver{
|
||||
cache: map[string][]string{},
|
||||
mu: sync.Mutex{},
|
||||
readOnly: false,
|
||||
resolver: reso,
|
||||
}
|
||||
}
|
||||
return reso
|
||||
}
|
||||
|
||||
// LookupHost implements Resolver.LookupHost
|
||||
func (r *CacheResolver) LookupHost(
|
||||
// MaybeWrapWithStaticDNSCache wraps the provided resolver with a resolver that
|
||||
// checks the given cache before issuing queries to the underlying DNS resolver.
|
||||
//
|
||||
// Bug: the returned resolver only applies caching to LookupHost and any other
|
||||
// lookup operation returns ErrNoDNSTransport to the caller.
|
||||
func MaybeWrapWithStaticDNSCache(cache map[string][]string, reso model.Resolver) model.Resolver {
|
||||
if len(cache) > 0 {
|
||||
reso = &cacheResolver{
|
||||
cache: cache,
|
||||
mu: sync.Mutex{},
|
||||
readOnly: true,
|
||||
resolver: reso,
|
||||
}
|
||||
}
|
||||
return reso
|
||||
}
|
||||
|
||||
// cacheResolver implements CachingResolver and StaticDNSCache.
|
||||
type cacheResolver struct {
|
||||
// cache is the underlying DNS cache.
|
||||
cache map[string][]string
|
||||
|
||||
// mu provides mutual exclusion.
|
||||
mu sync.Mutex
|
||||
|
||||
// readOnly means that we won't cache the result of successful resolutions.
|
||||
readOnly bool
|
||||
|
||||
// resolver is the underlying resolver.
|
||||
resolver model.Resolver
|
||||
}
|
||||
|
||||
var _ model.Resolver = &cacheResolver{}
|
||||
|
||||
// LookupHost implements model.Resolver.LookupHost
|
||||
func (r *cacheResolver) LookupHost(
|
||||
ctx context.Context, hostname string) ([]string, error) {
|
||||
if entry := r.Get(hostname); entry != nil {
|
||||
if entry := r.get(hostname); entry != nil {
|
||||
return entry, nil
|
||||
}
|
||||
entry, err := r.Resolver.LookupHost(ctx, hostname)
|
||||
entry, err := r.resolver.LookupHost(ctx, hostname)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !r.ReadOnly {
|
||||
r.Set(hostname, entry)
|
||||
if !r.readOnly {
|
||||
r.set(hostname, entry)
|
||||
}
|
||||
return entry, nil
|
||||
}
|
||||
|
||||
// Get gets the currently configured entry for domain, or nil
|
||||
func (r *CacheResolver) Get(domain string) []string {
|
||||
// get gets the currently configured entry for domain, or nil
|
||||
func (r *cacheResolver) get(domain string) []string {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.cache[domain]
|
||||
}
|
||||
|
||||
// Set allows to pre-populate the cache
|
||||
func (r *CacheResolver) Set(domain string, addresses []string) {
|
||||
// set sets a valid inside the cache iff readOnly is false.
|
||||
func (r *cacheResolver) set(domain string, addresses []string) {
|
||||
r.mu.Lock()
|
||||
if r.cache == nil {
|
||||
r.cache = make(map[string][]string)
|
||||
|
|
@ -47,3 +93,28 @@ func (r *CacheResolver) Set(domain string, addresses []string) {
|
|||
r.cache[domain] = addresses
|
||||
r.mu.Unlock()
|
||||
}
|
||||
|
||||
// Address implements model.Resolver.Address.
|
||||
func (r *cacheResolver) Address() string {
|
||||
return r.resolver.Address()
|
||||
}
|
||||
|
||||
// Network implements model.Resolver.Network.
|
||||
func (r *cacheResolver) Network() string {
|
||||
return r.resolver.Network()
|
||||
}
|
||||
|
||||
// CloseIdleConnections implements model.Resolver.CloseIdleConnections.
|
||||
func (r *cacheResolver) CloseIdleConnections() {
|
||||
r.resolver.CloseIdleConnections()
|
||||
}
|
||||
|
||||
// LookupHTTPS implements model.Resolver.LookupHTTPS.
|
||||
func (r *cacheResolver) LookupHTTPS(ctx context.Context, domain string) (*model.HTTPSSvc, error) {
|
||||
return nil, netxlite.ErrNoDNSTransport
|
||||
}
|
||||
|
||||
// LookupNS implements model.Resolver.LookupNS.
|
||||
func (r *cacheResolver) LookupNS(ctx context.Context, domain string) ([]*net.NS, error) {
|
||||
return nil, netxlite.ErrNoDNSTransport
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue