feat(netxlite): TLSDialer closes idle connections (#463)

We are proceeding with this plan of every major type being able to
close idle connections, which will simplify making DNS resolvers.

See https://github.com/ooni/probe/issues/1591.
This commit is contained in:
Simone Basso 2021-09-06 13:29:37 +02:00 committed by GitHub
parent 3caf5800a2
commit ef9592f75e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View File

@ -228,6 +228,11 @@ type TLSDialer struct {
TLSHandshaker TLSHandshaker
}
// CloseIdleConnection closes idle connections, if any.
func (d *TLSDialer) CloseIdleConnection() {
d.Dialer.CloseIdleConnections()
}
// DialTLSContext dials a TLS connection.
func (d *TLSDialer) DialTLSContext(ctx context.Context, network, address string) (net.Conn, error) {
host, port, err := net.SplitHostPort(address)

View File

@ -278,7 +278,22 @@ func TestTLSHandshakerLoggerFailure(t *testing.T) {
}
}
func TestTLSDialerFailureSplitHostPort(t *testing.T) {
func TestTLSDialerCloseIdleConnections(t *testing.T) {
var called bool
dialer := &TLSDialer{
Dialer: &mocks.Dialer{
MockCloseIdleConnections: func() {
called = true
},
},
}
dialer.CloseIdleConnection()
if !called {
t.Fatal("not called")
}
}
func TestTLSDialerDialTLSContextFailureSplitHostPort(t *testing.T) {
dialer := &TLSDialer{}
ctx := context.Background()
const address = "www.google.com" // missing port
@ -291,7 +306,7 @@ func TestTLSDialerFailureSplitHostPort(t *testing.T) {
}
}
func TestTLSDialerFailureDialing(t *testing.T) {
func TestTLSDialerDialTLSContextFailureDialing(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel() // immediately fail
dialer := TLSDialer{Dialer: defaultDialer}
@ -304,7 +319,7 @@ func TestTLSDialerFailureDialing(t *testing.T) {
}
}
func TestTLSDialerFailureHandshaking(t *testing.T) {
func TestTLSDialerDialTLSContextFailureHandshaking(t *testing.T) {
ctx := context.Background()
dialer := TLSDialer{
Config: &tls.Config{},
@ -328,7 +343,7 @@ func TestTLSDialerFailureHandshaking(t *testing.T) {
}
}
func TestTLSDialerSuccessHandshaking(t *testing.T) {
func TestTLSDialerDialTLSContextSuccessHandshaking(t *testing.T) {
ctx := context.Background()
dialer := TLSDialer{
Dialer: &mocks.Dialer{MockDialContext: func(ctx context.Context, network, address string) (net.Conn, error) {