Commit 57f5c221 authored by Eric's avatar Eric

优化复用连接代码

parent dce04aa5
...@@ -42,18 +42,27 @@ type SSHClient struct { ...@@ -42,18 +42,27 @@ type SSHClient struct {
} }
func (s *SSHClient) refCount() int { func (s *SSHClient) refCount() int {
if s.isClosed(){
return 0
}
s.mu.RLock() s.mu.RLock()
defer s.mu.RUnlock() defer s.mu.RUnlock()
return s.ref return s.ref
} }
func (s *SSHClient) increaseRef() { func (s *SSHClient) increaseRef() {
if s.isClosed(){
return
}
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
s.ref++ s.ref++
} }
func (s *SSHClient) decreaseRef() { func (s *SSHClient) decreaseRef() {
if s.isClosed(){
return
}
s.mu.Lock() s.mu.Lock()
defer s.mu.Unlock() defer s.mu.Unlock()
if s.ref == 0 { if s.ref == 0 {
...@@ -78,7 +87,7 @@ func (s *SSHClient) Close() error { ...@@ -78,7 +87,7 @@ func (s *SSHClient) Close() error {
return s.client.Close() return s.client.Close()
} }
func (s *SSHClient) IsClosed() bool { func (s *SSHClient) isClosed() bool {
select { select {
case <-s.closed: case <-s.closed:
return true return true
...@@ -102,7 +111,6 @@ func KeepAlive(c *SSHClient, closed <-chan struct{}, keepInterval time.Duration) ...@@ -102,7 +111,6 @@ func KeepAlive(c *SSHClient, closed <-chan struct{}, keepInterval time.Duration)
logger.Errorf("SSH client %p keep alive err: %s", c, err.Error()) logger.Errorf("SSH client %p keep alive err: %s", c, err.Error())
_ = c.Close() _ = c.Close()
RecycleClient(c) RecycleClient(c)
logger.Debugf("Recycle Client SSH client %p ", c)
return return
} }
} }
...@@ -305,16 +313,17 @@ func RecycleClient(client *SSHClient) { ...@@ -305,16 +313,17 @@ func RecycleClient(client *SSHClient) {
return return
} }
client.decreaseRef() client.decreaseRef()
logger.Debugf("SSH client %p ref -1. current ref: %d", client, client.refCount()) if client.refCount() == 0 {
if client.refCount() == 0 || client.IsClosed() {
clientLock.Lock() clientLock.Lock()
delete(sshClients, client.key) delete(sshClients, client.key)
clientLock.Unlock() clientLock.Unlock()
err := client.Close() err := client.Close()
if err != nil { if err != nil {
logger.Errorf("Failed to close SSH client %p err: %s ", client, err.Error()) logger.Errorf("Close SSH client %p err: %s ", client, err.Error())
} else { } else {
logger.Debugf("Success to close SSH client %p", client) logger.Infof("Success to close SSH client %p", client)
} }
}else {
logger.Debugf("SSH client %p ref -1. current ref: %d", client, client.refCount())
} }
} }
...@@ -489,11 +489,11 @@ func (u *UserSftp) GetSFTPAndRealPath(req requestMessage) (conn *SftpConn, realP ...@@ -489,11 +489,11 @@ func (u *UserSftp) GetSFTPAndRealPath(req requestMessage) (conn *SftpConn, realP
if su, ok := host.suMaps[req.su]; ok { if su, ok := host.suMaps[req.su]; ok {
key := fmt.Sprintf("%s@%s", su.Name, req.host) key := fmt.Sprintf("%s@%s", su.Name, req.host)
conn, ok := u.sftpClients[key] conn, ok := u.sftpClients[key]
if !ok || conn.conn.IsClosed(){ if !ok {
var err error var err error
conn, err = u.GetSftpClient(host.asset, su) conn, err = u.GetSftpClient(host.asset, su)
if err != nil { if err != nil {
logger.Debug("Get Sftp Client err: ", err.Error()) logger.Info("Get Sftp Client err: ", err.Error())
return nil, "" return nil, ""
} }
u.sftpClients[key] = conn u.sftpClients[key] = conn
...@@ -583,7 +583,7 @@ func (u *UserSftp) SendFTPLog(dataChan <-chan *model.FTPLog) { ...@@ -583,7 +583,7 @@ func (u *UserSftp) SendFTPLog(dataChan <-chan *model.FTPLog) {
if err == nil { if err == nil {
break break
} }
logger.Debugf("create FTP log err: %s", err.Error()) logger.Errorf("create FTP log err: %s", err.Error())
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment