From e985a73fb36436e53e3462813f777b389ef789fd Mon Sep 17 00:00:00 2001 From: Mikhail Faraponov <11322032+moredure@users.noreply.github.com> Date: Thu, 7 Apr 2022 21:32:27 +0300 Subject: [PATCH] Update tlsClientHandshake --- client.go | 43 ++++++++++++++----------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/client.go b/client.go index 5c40f782af..a311aaaa42 100644 --- a/client.go +++ b/client.go @@ -1994,41 +1994,26 @@ func (c *HostClient) cachedTLSConfig(addr string) *tls.Config { // ErrTLSHandshakeTimeout indicates there is a timeout from tls handshake. var ErrTLSHandshakeTimeout = errors.New("tls handshake timed out") -var timeoutErrorChPool sync.Pool - -func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, timeout time.Duration) (net.Conn, error) { - tc := AcquireTimer(timeout) - defer ReleaseTimer(tc) - - var ch chan error - chv := timeoutErrorChPool.Get() - if chv == nil { - chv = make(chan error) - } - ch = chv.(chan error) - defer timeoutErrorChPool.Put(chv) - +func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, deadline time.Time) (net.Conn, error) { conn := tls.Client(rawConn, tlsConfig) - - go func() { - ch <- conn.Handshake() - }() - - select { - case <-tc.C: + _ = conn.SetReadDeadline(deadline) + _ = conn.SetWriteDeadline(deadline) + err := conn.Handshake() + if netErr, ok := err.(net.Error); ok && netErr.Timeout() { rawConn.Close() - <-ch return nil, ErrTLSHandshakeTimeout - case err := <-ch: - if err != nil { - rawConn.Close() - return nil, err - } - return conn, nil } + if err != nil { + rawConn.Close() + return nil, err + } + _ = conn.SetReadDeadline(time.Time{}) + _ = conn.SetWriteDeadline(time.Time{}) + return conn, nil } func dialAddr(addr string, dial DialFunc, dialDualStack, isTLS bool, tlsConfig *tls.Config, timeout time.Duration) (net.Conn, error) { + deadline := time.Now().Add(timeout) if dial == nil { if dialDualStack { dial = DialDualStack @@ -2049,7 +2034,7 @@ func dialAddr(addr string, dial DialFunc, dialDualStack, isTLS bool, tlsConfig * if timeout == 0 { return tls.Client(conn, tlsConfig), nil } - return tlsClientHandshake(conn, tlsConfig, timeout) + return tlsClientHandshake(conn, tlsConfig, deadline) } return conn, nil }