8a0beee808
What do I mean by pivoting? Netx is currently organized by row: ``` | dialer | quicdialer | resolver | ... saving | | | | ... errorwrapping | | | | ... logging | | | | ... mocking/sys | | | | ... ``` Every row needs to implement saving, errorwrapping, logging, mocking (or adapting to the system or to some underlying library). This causes cross package dependencies and, in turn, complexity. For example, we need the `trace` package for supporting saving. And `dialer`, `quickdialer`, et al. need to depend on such a package. The same goes for errorwrapping. This arrangement further complicates testing. For example, I am currently working on https://github.com/ooni/probe/issues/1505 and I realize it need to repeat integration tests in multiple places. Let's say instead we pivot the above matrix as follows: ``` | saving | errorwrapping | logging | ... dialer | | | | ... quicdialer | | | | ... logging | | | | ... mocking/sys | | | | ... ... ``` In this way, now every row contains everything related to a specific action to perform. We can now share code without relying on extra support packages. What's more, we can write tests and, judding from the way in which things are made, it seems we only need integration testing in `errorwrapping` because it's where data quality matters whereas, in all other cases, unit testing is fine. I am going, therefore, to proceed with these changes and "pivot" `netx`. Hopefully, it won't be too painful.
79 lines
1.8 KiB
Go
79 lines
1.8 KiB
Go
package netxlite
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
// Resolver performs domain name resolutions.
|
|
type Resolver interface {
|
|
// LookupHost behaves like net.Resolver.LookupHost.
|
|
LookupHost(ctx context.Context, hostname string) (addrs []string, err error)
|
|
}
|
|
|
|
// ResolverSystem is the system resolver.
|
|
type ResolverSystem struct{}
|
|
|
|
var _ Resolver = ResolverSystem{}
|
|
|
|
// LookupHost implements Resolver.LookupHost.
|
|
func (r ResolverSystem) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
|
return net.DefaultResolver.LookupHost(ctx, hostname)
|
|
}
|
|
|
|
// Network implements Resolver.Network.
|
|
func (r ResolverSystem) Network() string {
|
|
return "system"
|
|
}
|
|
|
|
// Address implements Resolver.Address.
|
|
func (r ResolverSystem) Address() string {
|
|
return ""
|
|
}
|
|
|
|
// DefaultResolver is the resolver we use by default.
|
|
var DefaultResolver = ResolverSystem{}
|
|
|
|
// ResolverLogger is a resolver that emits events
|
|
type ResolverLogger struct {
|
|
Resolver
|
|
Logger Logger
|
|
}
|
|
|
|
var _ Resolver = ResolverLogger{}
|
|
|
|
// LookupHost returns the IP addresses of a host
|
|
func (r ResolverLogger) LookupHost(ctx context.Context, hostname string) ([]string, error) {
|
|
r.Logger.Debugf("resolve %s...", hostname)
|
|
start := time.Now()
|
|
addrs, err := r.Resolver.LookupHost(ctx, hostname)
|
|
stop := time.Now()
|
|
r.Logger.Debugf("resolve %s... (%+v, %+v) in %s", hostname, addrs, err, stop.Sub(start))
|
|
return addrs, err
|
|
}
|
|
|
|
type resolverNetworker interface {
|
|
Network() string
|
|
}
|
|
|
|
// Network implements Resolver.Network.
|
|
func (r ResolverLogger) Network() string {
|
|
if rn, ok := r.Resolver.(resolverNetworker); ok {
|
|
return rn.Network()
|
|
}
|
|
return "logger"
|
|
}
|
|
|
|
type resolverAddresser interface {
|
|
Address() string
|
|
}
|
|
|
|
// Address implements Resolver.Address.
|
|
func (r ResolverLogger) Address() string {
|
|
if ra, ok := r.Resolver.(resolverAddresser); ok {
|
|
return ra.Address()
|
|
}
|
|
return ""
|
|
}
|