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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 5 deletions

View File

@ -135,7 +135,8 @@ func (e *DefaultExplorer) getH3(h3URL *h3URL, headers map[string][]string) (*htt
tlsConf := &tls.Config{ tlsConf := &tls.Config{
NextProtos: []string{h3URL.proto}, NextProtos: []string{h3URL.proto},
} }
transport := netxlite.NewHTTP3Transport(dialer, tlsConf) transport := netxlite.NewHTTP3Transport(
netxlite.NewQUICDialerFromContextDialerAdapter(dialer), tlsConf)
// TODO(bassosimone): here we should use runtimex.PanicOnError // TODO(bassosimone): here we should use runtimex.PanicOnError
jarjar, _ := cookiejar.New(nil) jarjar, _ := cookiejar.New(nil)
clnt := &http.Client{ clnt := &http.Client{

View File

@ -10,5 +10,7 @@ import (
// //
// New code should use netxlite.NewHTTP3Transport instead. // New code should use netxlite.NewHTTP3Transport instead.
func NewHTTP3Transport(config Config) RoundTripper { func NewHTTP3Transport(config Config) RoundTripper {
return netxlite.NewHTTP3Transport(config.QUICDialer, config.TLSConfig) return netxlite.NewHTTP3Transport(
netxlite.NewQUICDialerFromContextDialerAdapter(config.QUICDialer),
config.TLSConfig)
} }

View File

@ -13,7 +13,7 @@ import (
// an http3.RoundTripper. This is necessary because the // an http3.RoundTripper. This is necessary because the
// http3.RoundTripper does not support DialContext. // http3.RoundTripper does not support DialContext.
type http3Dialer struct { type http3Dialer struct {
Dialer QUICContextDialer Dialer QUICDialer
} }
// dial is like QUICContextDialer.DialContext but without context. // dial is like QUICContextDialer.DialContext but without context.
@ -26,6 +26,7 @@ func (d *http3Dialer) dial(network, address string, tlsConfig *tls.Config,
// http3Transport is an HTTPTransport using the http3 protocol. // http3Transport is an HTTPTransport using the http3 protocol.
type http3Transport struct { type http3Transport struct {
child *http3.RoundTripper child *http3.RoundTripper
dialer QUICDialer
} }
var _ HTTPTransport = &http3Transport{} var _ HTTPTransport = &http3Transport{}
@ -38,13 +39,14 @@ func (txp *http3Transport) RoundTrip(req *http.Request) (*http.Response, error)
// CloseIdleConnections implements HTTPTransport.CloseIdleConnections. // CloseIdleConnections implements HTTPTransport.CloseIdleConnections.
func (txp *http3Transport) CloseIdleConnections() { func (txp *http3Transport) CloseIdleConnections() {
txp.child.Close() txp.child.Close()
txp.dialer.CloseIdleConnections()
} }
// NewHTTP3Transport creates a new HTTPTransport using http3. The // NewHTTP3Transport creates a new HTTPTransport using http3. The
// dialer argument MUST NOT be nil. If the tlsConfig argument is nil, // dialer argument MUST NOT be nil. If the tlsConfig argument is nil,
// then the code will use the default TLS configuration. // then the code will use the default TLS configuration.
func NewHTTP3Transport( func NewHTTP3Transport(
dialer QUICContextDialer, tlsConfig *tls.Config) HTTPTransport { dialer QUICDialer, tlsConfig *tls.Config) HTTPTransport {
return &http3Transport{ return &http3Transport{
child: &http3.RoundTripper{ child: &http3.RoundTripper{
Dial: (&http3Dialer{dialer}).dial, Dial: (&http3Dialer{dialer}).dial,
@ -55,5 +57,6 @@ func NewHTTP3Transport(
DisableCompression: true, DisableCompression: true,
TLSClientConfig: tlsConfig, TLSClientConfig: tlsConfig,
}, },
dialer: dialer,
} }
} }

View File

@ -6,6 +6,7 @@ import (
"testing" "testing"
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/netxlite/mocks"
) )
func TestHTTP3TransportWorks(t *testing.T) { func TestHTTP3TransportWorks(t *testing.T) {
@ -24,3 +25,18 @@ func TestHTTP3TransportWorks(t *testing.T) {
resp.Body.Close() resp.Body.Close()
txp.CloseIdleConnections() 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")
}
}