Skip to content

Commit 39fd89c

Browse files
committed
tls: Add handshake timeout to interface
1 parent 17a4b8d commit 39fd89c

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

common/tls/config.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/tls"
66
"net"
7+
"time"
78
)
89

910
type (
@@ -17,6 +18,8 @@ type Config interface {
1718
SetServerName(serverName string)
1819
NextProtos() []string
1920
SetNextProtos(nextProto []string)
21+
HandshakeTimeout() time.Duration
22+
SetHandshakeTimeout(timeout time.Duration)
2023
STDConfig() (*STDConfig, error)
2124
Client(conn net.Conn) (Conn, error)
2225
Clone() Config
@@ -51,6 +54,11 @@ type Conn interface {
5154
}
5255

5356
func ClientHandshake(ctx context.Context, conn net.Conn, config Config) (Conn, error) {
57+
if handshakeTimeout := config.HandshakeTimeout(); handshakeTimeout > 0 {
58+
var cancel context.CancelFunc
59+
ctx, cancel = context.WithTimeout(ctx, handshakeTimeout)
60+
defer cancel()
61+
}
5462
if compatServer, isCompat := config.(ConfigCompat); isCompat {
5563
return compatServer.ClientHandshake(ctx, conn)
5664
}
@@ -66,6 +74,11 @@ func ClientHandshake(ctx context.Context, conn net.Conn, config Config) (Conn, e
6674
}
6775

6876
func ServerHandshake(ctx context.Context, conn net.Conn, config ServerConfig) (Conn, error) {
77+
if handshakeTimeout := config.HandshakeTimeout(); handshakeTimeout > 0 {
78+
var cancel context.CancelFunc
79+
ctx, cancel = context.WithTimeout(ctx, handshakeTimeout)
80+
defer cancel()
81+
}
6982
if compatServer, isCompat := config.(ServerConfigCompat); isCompat {
7083
return compatServer.ServerHandshake(ctx, conn)
7184
}

0 commit comments

Comments
 (0)