From b4fad5db8437723a4765b59634d4ea4f2dc704bb 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 1/6] Update tlsClientHandshake --- client.go | 59 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/client.go b/client.go index 5c40f782af..e6378b1a72 100644 --- a/client.go +++ b/client.go @@ -1994,41 +1994,42 @@ 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: + err := conn.SetReadDeadline(deadline) + if err != nil { + rawConn.Close() + return nil, err + } + err = conn.SetWriteDeadline(deadline) + if err != nil { + rawConn.Close() + return nil, err + } + 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 + } + err = conn.SetReadDeadline(time.Time{}) + if err != nil { + rawConn.Close() + return nil, err + } + err = conn.SetWriteDeadline(time.Time{}) + if err != nil { + rawConn.Close() + return nil, err + } + 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 +2050,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 } From e54b935011b8655d65baea4d8eb464f611dac935 Mon Sep 17 00:00:00 2001 From: Mikhail Faraponov <11322032+moredure@users.noreply.github.com> Date: Sat, 9 Apr 2022 14:57:07 +0300 Subject: [PATCH 2/6] Update client.go --- client.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/client.go b/client.go index e6378b1a72..6ade58cf14 100644 --- a/client.go +++ b/client.go @@ -1994,35 +1994,35 @@ 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") -func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, deadline time.Time) (net.Conn, error) { - conn := tls.Client(rawConn, tlsConfig) - err := conn.SetReadDeadline(deadline) +func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, deadline time.Time) (conn net.Conn, err error) { + defer func() { + if err != nil { + rawConn.Close() + return nil, err + } + }() + conn = tls.Client(rawConn, tlsConfig) + err = conn.SetReadDeadline(deadline) if err != nil { - rawConn.Close() return nil, err } err = conn.SetWriteDeadline(deadline) if err != nil { - rawConn.Close() return nil, err } err = conn.Handshake() if netErr, ok := err.(net.Error); ok && netErr.Timeout() { - rawConn.Close() return nil, ErrTLSHandshakeTimeout } if err != nil { - rawConn.Close() return nil, err } err = conn.SetReadDeadline(time.Time{}) if err != nil { - rawConn.Close() return nil, err } err = conn.SetWriteDeadline(time.Time{}) if err != nil { - rawConn.Close() return nil, err } return conn, nil From b35c1717d8524e9ad17ba1d7056e0321cf590771 Mon Sep 17 00:00:00 2001 From: Mikhail Faraponov <11322032+moredure@users.noreply.github.com> Date: Sat, 9 Apr 2022 15:18:04 +0300 Subject: [PATCH 3/6] Update client.go --- client.go | 1 - 1 file changed, 1 deletion(-) diff --git a/client.go b/client.go index 6ade58cf14..918503947d 100644 --- a/client.go +++ b/client.go @@ -1998,7 +1998,6 @@ func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, deadline time.T defer func() { if err != nil { rawConn.Close() - return nil, err } }() conn = tls.Client(rawConn, tlsConfig) From a8742bcb13808a13235cda6a551aea9bd29e11bf Mon Sep 17 00:00:00 2001 From: Mikhail Faraponov <11322032+moredure@users.noreply.github.com> Date: Sat, 9 Apr 2022 15:35:08 +0300 Subject: [PATCH 4/6] Update client.go --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index 918503947d..57b2060619 100644 --- a/client.go +++ b/client.go @@ -1994,7 +1994,7 @@ 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") -func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, deadline time.Time) (conn net.Conn, err error) { +func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, deadline time.Time) (_ net.Conn, err error) { defer func() { if err != nil { rawConn.Close() From 3af4f78ad5cdbb067ca6f5abec231a69ddb2b7c8 Mon Sep 17 00:00:00 2001 From: Mikhail Faraponov <11322032+moredure@users.noreply.github.com> Date: Sat, 9 Apr 2022 15:36:00 +0300 Subject: [PATCH 5/6] Update client.go --- client.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index 57b2060619..3c10aa3a57 100644 --- a/client.go +++ b/client.go @@ -1994,14 +1994,14 @@ 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") -func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, deadline time.Time) (_ net.Conn, err error) { +func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, deadline time.Time) (_ net.Conn, retErr error) { defer func() { - if err != nil { + if retErr != nil { rawConn.Close() } }() - conn = tls.Client(rawConn, tlsConfig) - err = conn.SetReadDeadline(deadline) + conn := tls.Client(rawConn, tlsConfig) + err := conn.SetReadDeadline(deadline) if err != nil { return nil, err } From 4f63cfe511ebfdfbbdff038921224f98067be46a Mon Sep 17 00:00:00 2001 From: Mikhail Faraponov <11322032+moredure@users.noreply.github.com> Date: Sat, 9 Apr 2022 23:02:57 +0300 Subject: [PATCH 6/6] Changes according to the review --- client.go | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index 3c10aa3a57..7ffae8b6f4 100644 --- a/client.go +++ b/client.go @@ -2001,11 +2001,7 @@ func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, deadline time.T } }() conn := tls.Client(rawConn, tlsConfig) - err := conn.SetReadDeadline(deadline) - if err != nil { - return nil, err - } - err = conn.SetWriteDeadline(deadline) + err := conn.SetDeadline(deadline) if err != nil { return nil, err } @@ -2016,11 +2012,7 @@ func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, deadline time.T if err != nil { return nil, err } - err = conn.SetReadDeadline(time.Time{}) - if err != nil { - return nil, err - } - err = conn.SetWriteDeadline(time.Time{}) + err = conn.SetDeadline(time.Time{}) if err != nil { return nil, err }