Skip to content

Commit

Permalink
improve keep alive
Browse files Browse the repository at this point in the history
  • Loading branch information
lonnywong committed Feb 4, 2024
1 parent acbd5b5 commit f44addf
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions tssh/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,8 @@ func dialWithTimeout(client *ssh.Client, network, addr string, timeout time.Dura
return
}

var lastServerAliveTime atomic.Pointer[time.Time]

type connWithTimeout struct {
net.Conn
timeout time.Duration
Expand All @@ -951,7 +953,12 @@ type connWithTimeout struct {

func (c *connWithTimeout) Read(b []byte) (n int, err error) {
if !c.firstRead {
return c.Conn.Read(b)
n, err = c.Conn.Read(b)
if err != nil {
now := time.Now()
lastServerAliveTime.Store(&now)
}
return
}
done := make(chan struct{}, 1)
go func() {
Expand Down Expand Up @@ -1109,10 +1116,15 @@ func keepAlive(client *ssh.Client, args *sshArgs) {
}

go func() {
t := time.NewTicker(time.Duration(serverAliveInterval) * time.Second)
intervalTime := time.Duration(serverAliveInterval) * time.Second
t := time.NewTicker(intervalTime)
defer t.Stop()
n := 0
for range t.C {
if lastTime := lastServerAliveTime.Load(); lastTime != nil && time.Since(*lastTime) < intervalTime {
n = 0
continue
}
if _, _, err := client.SendRequest("keepalive@trzsz-ssh", true, nil); err != nil {
n++
if n >= serverAliveCountMax {
Expand Down

0 comments on commit f44addf

Please sign in to comment.