Skip to content

Commit

Permalink
Merge pull request #115 from hongshengjie/master
Browse files Browse the repository at this point in the history
bufix: add tlsHandshake timeout to prevent readtimeout cause dead loop
  • Loading branch information
AlexStocks authored Mar 16, 2024
2 parents 71b8185 + 4033bd3 commit a5233ed
Showing 1 changed file with 15 additions and 0 deletions.
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

0 comments on commit a5233ed

Please sign in to comment.