From c6bf1a210b98474e564b7a9534d5be58d6e05814 Mon Sep 17 00:00:00 2001 From: Mikhail Faraponov <11322032+moredure@users.noreply.github.com> Date: Mon, 11 Apr 2022 13:39:12 +0300 Subject: [PATCH] crypto/tls: avoid creating handshake context on each write --- src/crypto/tls/conn.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/crypto/tls/conn.go b/src/crypto/tls/conn.go index fba36d30100736..8375052708ce69 100644 --- a/src/crypto/tls/conn.go +++ b/src/crypto/tls/conn.go @@ -1403,6 +1403,16 @@ func (c *Conn) HandshakeContext(ctx context.Context) error { } func (c *Conn) handshakeContext(ctx context.Context) (ret error) { + c.handshakeMutex.Lock() + defer c.handshakeMutex.Unlock() + + if err := c.handshakeErr; err != nil { + return err + } + if c.handshakeComplete() { + return nil + } + handshakeCtx, cancel := context.WithCancel(ctx) // Note: defer this before starting the "interrupter" goroutine // so that we can tell the difference between the input being canceled and @@ -1436,16 +1446,6 @@ func (c *Conn) handshakeContext(ctx context.Context) (ret error) { }() } - c.handshakeMutex.Lock() - defer c.handshakeMutex.Unlock() - - if err := c.handshakeErr; err != nil { - return err - } - if c.handshakeComplete() { - return nil - } - c.in.Lock() defer c.in.Unlock()