fix(netxlite): http3 propagates CloseIdleConnections to its dialer (#471)

With this change, we are now able to change more dependent code to simplify
the way in which we create and manage resolvers.

See https://github.com/ooni/probe/issues/1591
This commit is contained in:
Simone Basso
2021-09-06 21:52:00 +02:00
committed by GitHub
parent bdad392b61
commit b9c4ad0b2b
4 changed files with 27 additions and 5 deletions
+6 -3
View File
@@ -13,7 +13,7 @@ import (
// an http3.RoundTripper. This is necessary because the
// http3.RoundTripper does not support DialContext.
type http3Dialer struct {
Dialer QUICContextDialer
Dialer QUICDialer
}
// dial is like QUICContextDialer.DialContext but without context.
@@ -25,7 +25,8 @@ func (d *http3Dialer) dial(network, address string, tlsConfig *tls.Config,
// http3Transport is an HTTPTransport using the http3 protocol.
type http3Transport struct {
child *http3.RoundTripper
child *http3.RoundTripper
dialer QUICDialer
}
var _ HTTPTransport = &http3Transport{}
@@ -38,13 +39,14 @@ func (txp *http3Transport) RoundTrip(req *http.Request) (*http.Response, error)
// CloseIdleConnections implements HTTPTransport.CloseIdleConnections.
func (txp *http3Transport) CloseIdleConnections() {
txp.child.Close()
txp.dialer.CloseIdleConnections()
}
// NewHTTP3Transport creates a new HTTPTransport using http3. The
// dialer argument MUST NOT be nil. If the tlsConfig argument is nil,
// then the code will use the default TLS configuration.
func NewHTTP3Transport(
dialer QUICContextDialer, tlsConfig *tls.Config) HTTPTransport {
dialer QUICDialer, tlsConfig *tls.Config) HTTPTransport {
return &http3Transport{
child: &http3.RoundTripper{
Dial: (&http3Dialer{dialer}).dial,
@@ -55,5 +57,6 @@ func NewHTTP3Transport(
DisableCompression: true,
TLSClientConfig: tlsConfig,
},
dialer: dialer,
}
}
+16
View File
@@ -6,6 +6,7 @@ import (
"testing"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
)
func TestHTTP3TransportWorks(t *testing.T) {
@@ -24,3 +25,18 @@ func TestHTTP3TransportWorks(t *testing.T) {
resp.Body.Close()
txp.CloseIdleConnections()
}
func TestHTTP3TransportClosesIdleConnections(t *testing.T) {
var called bool
d := &mocks.QUICDialer{
MockCloseIdleConnections: func() {
called = true
},
}
txp := NewHTTP3Transport(d, &tls.Config{})
client := &http.Client{Transport: txp}
client.CloseIdleConnections()
if !called {
t.Fatal("not called")
}
}