Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
K
koko
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
koko
Commits
57f5c221
Commit
57f5c221
authored
Sep 12, 2019
by
Eric
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化复用连接代码
parent
dce04aa5
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
9 deletions
+18
-9
connmanager.go
pkg/srvconn/connmanager.go
+15
-6
sftpconn.go
pkg/srvconn/sftpconn.go
+3
-3
No files found.
pkg/srvconn/connmanager.go
View file @
57f5c221
...
...
@@ -42,18 +42,27 @@ type SSHClient struct {
}
func
(
s
*
SSHClient
)
refCount
()
int
{
if
s
.
isClosed
(){
return
0
}
s
.
mu
.
RLock
()
defer
s
.
mu
.
RUnlock
()
return
s
.
ref
}
func
(
s
*
SSHClient
)
increaseRef
()
{
if
s
.
isClosed
(){
return
}
s
.
mu
.
Lock
()
defer
s
.
mu
.
Unlock
()
s
.
ref
++
}
func
(
s
*
SSHClient
)
decreaseRef
()
{
if
s
.
isClosed
(){
return
}
s
.
mu
.
Lock
()
defer
s
.
mu
.
Unlock
()
if
s
.
ref
==
0
{
...
...
@@ -78,7 +87,7 @@ func (s *SSHClient) Close() error {
return
s
.
client
.
Close
()
}
func
(
s
*
SSHClient
)
I
sClosed
()
bool
{
func
(
s
*
SSHClient
)
i
sClosed
()
bool
{
select
{
case
<-
s
.
closed
:
return
true
...
...
@@ -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
())
_
=
c
.
Close
()
RecycleClient
(
c
)
logger
.
Debugf
(
"Recycle Client SSH client %p "
,
c
)
return
}
}
...
...
@@ -305,16 +313,17 @@ func RecycleClient(client *SSHClient) {
return
}
client
.
decreaseRef
()
logger
.
Debugf
(
"SSH client %p ref -1. current ref: %d"
,
client
,
client
.
refCount
())
if
client
.
refCount
()
==
0
||
client
.
IsClosed
()
{
if
client
.
refCount
()
==
0
{
clientLock
.
Lock
()
delete
(
sshClients
,
client
.
key
)
clientLock
.
Unlock
()
err
:=
client
.
Close
()
if
err
!=
nil
{
logger
.
Errorf
(
"
Failed to c
lose SSH client %p err: %s "
,
client
,
err
.
Error
())
logger
.
Errorf
(
"
C
lose SSH client %p err: %s "
,
client
,
err
.
Error
())
}
else
{
logger
.
Debug
f
(
"Success to close SSH client %p"
,
client
)
logger
.
Info
f
(
"Success to close SSH client %p"
,
client
)
}
}
else
{
logger
.
Debugf
(
"SSH client %p ref -1. current ref: %d"
,
client
,
client
.
refCount
())
}
}
pkg/srvconn/sftpconn.go
View file @
57f5c221
...
...
@@ -489,11 +489,11 @@ func (u *UserSftp) GetSFTPAndRealPath(req requestMessage) (conn *SftpConn, realP
if
su
,
ok
:=
host
.
suMaps
[
req
.
su
];
ok
{
key
:=
fmt
.
Sprintf
(
"%s@%s"
,
su
.
Name
,
req
.
host
)
conn
,
ok
:=
u
.
sftpClients
[
key
]
if
!
ok
||
conn
.
conn
.
IsClosed
()
{
if
!
ok
{
var
err
error
conn
,
err
=
u
.
GetSftpClient
(
host
.
asset
,
su
)
if
err
!=
nil
{
logger
.
Debug
(
"Get Sftp Client err: "
,
err
.
Error
())
logger
.
Info
(
"Get Sftp Client err: "
,
err
.
Error
())
return
nil
,
""
}
u
.
sftpClients
[
key
]
=
conn
...
...
@@ -583,7 +583,7 @@ func (u *UserSftp) SendFTPLog(dataChan <-chan *model.FTPLog) {
if
err
==
nil
{
break
}
logger
.
Debug
f
(
"create FTP log err: %s"
,
err
.
Error
())
logger
.
Error
f
(
"create FTP log err: %s"
,
err
.
Error
())
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment