refactor: merge dnsx and errorsx into netxlite (#517)

When preparing a tutorial for netxlite, I figured it is easier
to tell people "hey, this is the package you should use for all
low-level networking stuff" rather than introducing people to
a set of packages working together where some piece of functionality
is here and some other piece is there.

Part of https://github.com/ooni/probe/issues/1591
This commit is contained in:
Simone Basso
2021-09-28 12:42:01 +02:00
committed by GitHub
parent de130d249c
commit 6d3a4f1db8
169 changed files with 575 additions and 671 deletions
+3 -3
View File
@@ -9,7 +9,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/handlers"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/oldhttptransport"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
"golang.org/x/net/http2"
)
@@ -82,10 +82,10 @@ func (t *HTTPTransport) RoundTrip(
resp, err = t.roundTripper.RoundTrip(req)
// For safety wrap the error as modelx.HTTPRoundTripOperation but this
// will only be used if the error chain does not contain any
// other major operation failure. See errorsx.ErrWrapper.
// other major operation failure. See netxlite.ErrWrapper.
err = errorsxlegacy.SafeErrWrapperBuilder{
Error: err,
Operation: errorsx.HTTPRoundTripOperation,
Operation: netxlite.HTTPRoundTripOperation,
}.MaybeBuild()
return resp, err
}
+4 -5
View File
@@ -13,8 +13,7 @@ import (
"time"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func dowithclient(t *testing.T, client *netx.HTTPClient) {
@@ -24,7 +23,7 @@ func dowithclient(t *testing.T, client *netx.HTTPClient) {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = iox.ReadAllContext(context.Background(), resp.Body)
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -126,7 +125,7 @@ func httpProxyTestMain(t *testing.T, client *http.Client, expect int) {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = iox.ReadAllContext(context.Background(), resp.Body)
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -148,7 +147,7 @@ func TestHTTPTransportTimeout(t *testing.T) {
if err == nil {
t.Fatal("expected an error here")
}
if !strings.HasSuffix(err.Error(), errorsx.FailureGenericTimeoutError) {
if !strings.HasSuffix(err.Error(), netxlite.FailureGenericTimeoutError) {
t.Fatal("not the error we expected")
}
if resp != nil {
@@ -8,7 +8,7 @@ import (
"testing"
"time"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestNewTLSConnectionState(t *testing.T) {
@@ -61,7 +61,7 @@ func TestMeasurementRootWithMeasurementRootPanic(t *testing.T) {
func TestErrWrapperPublicAPI(t *testing.T) {
child := errors.New("mocked error")
wrapper := &errorsx.ErrWrapper{
wrapper := &netxlite.ErrWrapper{
Failure: "moobar",
WrappedErr: child,
}
@@ -5,7 +5,7 @@ import (
"net/http"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestBodyTracerSuccess(t *testing.T) {
@@ -17,7 +17,7 @@ func TestBodyTracerSuccess(t *testing.T) {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = iox.ReadAllContext(context.Background(), resp.Body)
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -5,7 +5,7 @@ import (
"net/http"
"testing"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestGood(t *testing.T) {
@@ -17,7 +17,7 @@ func TestGood(t *testing.T) {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = iox.ReadAllContext(context.Background(), resp.Body)
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -13,8 +13,7 @@ import (
"github.com/ooni/probe-cli/v3/internal/atomicx"
errorsxlegacy "github.com/ooni/probe-cli/v3/internal/engine/legacy/errorsx"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
// TraceTripper performs single HTTP transactions.
@@ -28,7 +27,7 @@ type TraceTripper struct {
func NewTraceTripper(roundTripper http.RoundTripper) *TraceTripper {
return &TraceTripper{
readAllErrs: &atomicx.Int64{},
readAllContext: iox.ReadAllContext,
readAllContext: netxlite.ReadAllContext,
roundTripper: roundTripper,
}
}
@@ -84,7 +83,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
var (
err error
majorOp = errorsx.HTTPRoundTripOperation
majorOp = netxlite.HTTPRoundTripOperation
majorOpMu sync.Mutex
requestBody []byte
requestHeaders = http.Header{}
@@ -104,7 +103,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
tracer := &httptrace.ClientTrace{
TLSHandshakeStart: func() {
majorOpMu.Lock()
majorOp = errorsx.TLSHandshakeOperation
majorOp = netxlite.TLSHandshakeOperation
majorOpMu.Unlock()
// Event emitted by net/http when DialTLS is not
// configured in the http.Transport
@@ -119,7 +118,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
// less confusing to users to see the wrapped name
err = errorsxlegacy.SafeErrWrapperBuilder{
Error: err,
Operation: errorsx.TLSHandshakeOperation,
Operation: netxlite.TLSHandshakeOperation,
}.MaybeBuild()
durationSinceBeginning := time.Since(root.Beginning)
// Event emitted by net/http when DialTLS is not
@@ -134,7 +133,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
},
GotConn: func(info httptrace.GotConnInfo) {
majorOpMu.Lock()
majorOp = errorsx.HTTPRoundTripOperation
majorOp = netxlite.HTTPRoundTripOperation
majorOpMu.Unlock()
root.Handler.OnMeasurement(modelx.Measurement{
HTTPConnectionReady: &modelx.HTTPConnectionReadyEvent{
@@ -174,7 +173,7 @@ func (t *TraceTripper) RoundTrip(req *http.Request) (*http.Response, error) {
// less confusing to users to see the wrapped name
err := errorsxlegacy.SafeErrWrapperBuilder{
Error: info.Err,
Operation: errorsx.HTTPRoundTripOperation,
Operation: netxlite.HTTPRoundTripOperation,
}.MaybeBuild()
root.Handler.OnMeasurement(modelx.Measurement{
HTTPRequestDone: &modelx.HTTPRequestDoneEvent{
@@ -13,7 +13,7 @@ import (
"github.com/miekg/dns"
"github.com/ooni/probe-cli/v3/internal/engine/legacy/netx/modelx"
"github.com/ooni/probe-cli/v3/internal/netxlite/iox"
"github.com/ooni/probe-cli/v3/internal/netxlite"
)
func TestTraceTripperSuccess(t *testing.T) {
@@ -25,7 +25,7 @@ func TestTraceTripperSuccess(t *testing.T) {
t.Fatal(err)
}
defer resp.Body.Close()
_, err = iox.ReadAllContext(context.Background(), resp.Body)
_, err = netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
@@ -156,7 +156,7 @@ func TestTraceTripperWithCorrectSnaps(t *testing.T) {
// Read the whole response body, parse it as valid DNS
// reply and verify we obtained what we expected
replyData, err := iox.ReadAllContext(context.Background(), resp.Body)
replyData, err := netxlite.ReadAllContext(context.Background(), resp.Body)
if err != nil {
t.Fatal(err)
}
+2 -1
View File
@@ -118,7 +118,8 @@ func newResolver(
if network == "udp" {
// Same rationale as above: avoid possible endless loop
return newResolverWrapper(beginning, handler, newResolverUDP(
newDialer(beginning, handler), withPort(address, "53"),
netxlite.NewDialerLegacyAdapter(newDialer(beginning, handler)),
withPort(address, "53"),
)), nil
}
return nil, errors.New("resolver.New: unsupported network value")