refactor: start refactoring session resolver (#807)

This diff addresses the following points of https://github.com/ooni/probe/issues/2135:

- [x] the `childResolver` type is useless and we can use `model.Resolver` directly;
- [x] we should use `model/mocks` instead of custom fakes;
- [x] we should not use `log.Log` rather we should use `model.DiscardLogger`;
- [x] make `timeLimitedLookup` easier to test with a `-short` tests;
- [x] ensure `timeLimitedLookup` returns as soon as its context expires regardless of the child resolver;

Subsequent diffs will address more points mentioned in there.
This commit is contained in:
Simone Basso 2022-06-08 14:06:22 +02:00 committed by GitHub
commit fe29b432e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 126 additions and 84 deletions

View file

@ -5,7 +5,6 @@ import (
"strings"
"time"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx"
"github.com/ooni/probe-cli/v3/internal/model"
@ -71,15 +70,12 @@ func (r *Resolver) byteCounter() *bytecounter.Counter {
// logger returns the configured logger or a default
func (r *Resolver) logger() model.Logger {
if r.Logger != nil {
return r.Logger
}
return log.Log
return model.ValidLoggerOrDefault(r.Logger)
}
// newresolver creates a new resolver with the given config and URL. This is
// where we expand http3 to https and set the h3 options.
func (r *Resolver) newresolver(URL string) (childResolver, error) {
func (r *Resolver) newresolver(URL string) (model.Resolver, error) {
h3 := strings.HasPrefix(URL, "http3://")
if h3 {
URL = strings.Replace(URL, "http3://", "https://", 1)
@ -95,7 +91,7 @@ func (r *Resolver) newresolver(URL string) (childResolver, error) {
// getresolver returns a resolver with the given URL. This function caches
// already allocated resolvers so we only allocate them once.
func (r *Resolver) getresolver(URL string) (childResolver, error) {
func (r *Resolver) getresolver(URL string) (model.Resolver, error) {
defer r.mu.Unlock()
r.mu.Lock()
if re, found := r.res[URL]; found {
@ -106,7 +102,7 @@ func (r *Resolver) getresolver(URL string) (childResolver, error) {
return nil, err // config err?
}
if r.res == nil {
r.res = make(map[string]childResolver)
r.res = make(map[string]model.Resolver)
}
r.res[URL] = re
return re, nil