File tree Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Original file line number Diff line number Diff line change @@ -32,6 +32,7 @@ type Conn struct {
32
32
33
33
// handshakeStatus is 1 if the connection is currently transferring
34
34
// application data (i.e. is not currently processing a handshake).
35
+ // handshakeStatus == 1 implies handshakeErr == nil.
35
36
// This field is only to be accessed with sync/atomic.
36
37
handshakeStatus uint32
37
38
// constant after handshake; protected by handshakeMutex
@@ -1405,6 +1406,13 @@ func (c *Conn) HandshakeContext(ctx context.Context) error {
1405
1406
}
1406
1407
1407
1408
func (c * Conn ) handshakeContext (ctx context.Context ) (ret error ) {
1409
+ // Fast sync/atomic-based exit if there is no handshake in flight and the
1410
+ // last one succeeded without an error. Avoids the expensive context setup
1411
+ // and mutex for most Read and Write calls.
1412
+ if c .handshakeComplete () {
1413
+ return nil
1414
+ }
1415
+
1408
1416
handshakeCtx , cancel := context .WithCancel (ctx )
1409
1417
// Note: defer this before starting the "interrupter" goroutine
1410
1418
// so that we can tell the difference between the input being canceled and
@@ -1463,6 +1471,9 @@ func (c *Conn) handshakeContext(ctx context.Context) (ret error) {
1463
1471
if c .handshakeErr == nil && ! c .handshakeComplete () {
1464
1472
c .handshakeErr = errors .New ("tls: internal error: handshake should have had a result" )
1465
1473
}
1474
+ if c .handshakeErr != nil && c .handshakeComplete () {
1475
+ panic ("tls: internal error: handshake returned an error but is marked successful" )
1476
+ }
1466
1477
1467
1478
return c .handshakeErr
1468
1479
}
You can’t perform that action at this time.
0 commit comments