Skip to content

Commit

Permalink
Merge pull request #287 from jumpserver/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
LeeEirc authored May 14, 2020
2 parents 44d519b + 46a8505 commit d85989d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ RUN set -ex; \
apt-key list > /dev/null

ENV MYSQL_MAJOR 8.0
ENV MYSQL_VERSION 8.0.19-1debian9
ENV MYSQL_VERSION 8.0.20-1debian9
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/mysql/apt/debian stretch mysql-${MYSQL_MAJOR}" > /etc/apt/sources.list.d/mysql.list
RUN apt-get update && apt-get install -y gdb ca-certificates mysql-community-client="${MYSQL_VERSION}" && rm -rf /var/lib/apt/lists/*

Expand Down
11 changes: 8 additions & 3 deletions pkg/srvconn/connmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ func (s *sshClient) Close() error {
return nil
}
s.mu.Lock()
defer s.mu.Unlock()
s.ref--
var needClosed bool
if s.ref <= 0 {
deleteClientFromCache(s.key, s)
needClosed = true
}
s.mu.Unlock()
if needClosed {
return s.close()
}
return nil
Expand All @@ -128,6 +131,9 @@ func (s *sshClient) close() error {
default:
close(s.closed)
}
if s.key != "" {
deleteClientFromCache(s.key, s)
}
s.ref = 0
if s.proxyConn != nil {
_ = s.proxyConn.Close()
Expand Down Expand Up @@ -169,7 +175,6 @@ func KeepAlive(c *sshClient, closed <-chan struct{}, keepInterval time.Duration)
logger.Errorf("SSH client %s keep alive err: %s, retry count: %d", c, err.Error(), errCount)
if errCount >= retryCount {
logger.Errorf("SSH client %s keep alive err count exceed max count: %d and close it", c, retryCount)
deleteClientFromCache(c.key, c)
_ = c.close()
return
}
Expand Down
19 changes: 8 additions & 11 deletions pkg/srvconn/sshclients.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package srvconn
import (
"strings"
"sync"
"time"

"github.com/jumpserver/koko/pkg/logger"
)
Expand All @@ -16,7 +17,7 @@ type UserSSHClient struct {
func (u *UserSSHClient) AddClient(client *sshClient) {
u.mu.Lock()
defer u.mu.Unlock()
u.clients[client] = 0
u.clients[client] = time.Now().UnixNano()
logger.Infof("Store new client(%s) remain %d", client, len(u.clients))
}

Expand All @@ -30,19 +31,15 @@ func (u *UserSSHClient) DeleteClient(client *sshClient) {
func (u *UserSSHClient) GetClient() *sshClient {
u.mu.Lock()
defer u.mu.Unlock()
var client *sshClient
var ref int
if len(u.clients) == 0 {
return nil
}
for item := range u.clients {
if ref == 0 {
ref = item.RefCount()
client = item
continue
}
if item.RefCount() < ref {
ref = item.RefCount()

var client *sshClient
var latest int64
for item, timestamp := range u.clients {
if timestamp > latest {
latest = timestamp
client = item
}
}
Expand Down

0 comments on commit d85989d

Please sign in to comment.