Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bufix: add tlsHandshake timeout to prevent readtimeout cause dead loop #115

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package getty
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -55,6 +56,8 @@ const (
defaultWSSessionName = "ws-session"
defaultWSSSessionName = "wss-session"
outputFormat = "session %s, Read Bytes: %d, Write Bytes: %d, Read Pkgs: %d, Write Pkgs: %d"

defaultTLSHandshakeTimeout = time.Second * 3
)

var defaultTimerWheel *gxtime.TimerWheel
Expand Down Expand Up @@ -636,6 +639,18 @@ func (s *session) handleTCPPackage() error {
pktBuf = gxbytes.NewBuffer(nil)

conn = s.Connection.(*gettyTCPConn)
if tlsConn, ok := conn.conn.(*tls.Conn); ok {
tlsHandshaketime := defaultTLSHandshakeTimeout
if s.ReadTimeout() > 0 {
tlsHandshaketime = s.ReadTimeout()
}
ctx, cancel := context.WithTimeout(context.Background(), tlsHandshaketime)
defer cancel()
if err := tlsConn.HandshakeContext(ctx); err != nil {
log.Errorf("[tlsConn.HandshakeContext] = error:%+v", err)
return perrors.Wrap(err, "tlsConn.HandshakeContext")
}
}
for {
if s.IsClosed() {
err = nil
Expand Down
Loading