aboutsummaryrefslogtreecommitdiff
path: root/src/crypto/tls/conn.go
diff options
context:
space:
mode:
authorDmitri Shuralyov <dmitshur@google.com>2022-06-01 17:42:39 +0000
committerDmitri Shuralyov <dmitshur@google.com>2022-06-01 17:42:39 +0000
commit4658e6e324a85b0076f66acdab77799ffa9ac7bb (patch)
treef3b4de12074d583057060fdf35d91b6591859644 /src/crypto/tls/conn.go
parent6b07de790c08967d0dbe3a36ec86f3d13e1cfcbd (diff)
parent26cdea3acca29db94541236f0037a20aa22ce2d7 (diff)
downloadgo-4658e6e324a85b0076f66acdab77799ffa9ac7bb.tar.gz
go-4658e6e324a85b0076f66acdab77799ffa9ac7bb.zip
[dev.boringcrypto.go1.17] all: merge go1.17.11 into dev.boringcrypto.go1.17
Change-Id: I563433b2d02a5abea610a1561139d0980d5c2102
Diffstat (limited to 'src/crypto/tls/conn.go')
-rw-r--r--src/crypto/tls/conn.go11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/crypto/tls/conn.go b/src/crypto/tls/conn.go
index 969f357834..d0a2550cbc 100644
--- a/src/crypto/tls/conn.go
+++ b/src/crypto/tls/conn.go
@@ -32,6 +32,7 @@ type Conn struct {
// handshakeStatus is 1 if the connection is currently transferring
// application data (i.e. is not currently processing a handshake).
+ // handshakeStatus == 1 implies handshakeErr == nil.
// This field is only to be accessed with sync/atomic.
handshakeStatus uint32
// constant after handshake; protected by handshakeMutex
@@ -1396,6 +1397,13 @@ func (c *Conn) HandshakeContext(ctx context.Context) error {
}
func (c *Conn) handshakeContext(ctx context.Context) (ret error) {
+ // Fast sync/atomic-based exit if there is no handshake in flight and the
+ // last one succeeded without an error. Avoids the expensive context setup
+ // and mutex for most Read and Write calls.
+ 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
@@ -1454,6 +1462,9 @@ func (c *Conn) handshakeContext(ctx context.Context) (ret error) {
if c.handshakeErr == nil && !c.handshakeComplete() {
c.handshakeErr = errors.New("tls: internal error: handshake should have had a result")
}
+ if c.handshakeErr != nil && c.handshakeComplete() {
+ panic("tls: internal error: handshake returned an error but is marked successful")
+ }
return c.handshakeErr
}