From a64906e38470769c00af0342444ac4925007d460 Mon Sep 17 00:00:00 2001 From: fangdingjun Date: Thu, 5 Oct 2017 17:20:15 +0800 Subject: [PATCH] fix keepalive issue --- client.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index d94b396..f3dfe19 100644 --- a/client.go +++ b/client.go @@ -281,13 +281,23 @@ func (cc *Client) keepAlive(interval time.Duration, maxCount int) { for { select { case <-c.C: - _, _, err := cc.sshConn.SendRequest("keepalive@openssh.org", true, nil) - if err != nil { - Log(DEBUG, "keep alive error: %s", err.Error()) + resCh := make(chan error, 1) + go func(resCh chan error) { + _, _, err := cc.sshConn.SendRequest("keepalive@openssh.org", true, nil) + resCh <- err + }(resCh) + select { + case err := <-resCh: + if err != nil { + Log(DEBUG, "keep alive error: %s", err.Error()) + count++ + } else { + count = 0 + } + case <-time.After(3 * time.Second): count++ - } else { - count = 0 } + if count >= maxCount { cc.err = fmt.Errorf("keep alive detects connection hang up") Log(ERROR, "keep alive hit max count, exit")