-
Notifications
You must be signed in to change notification settings - Fork 1
/
sftp.go
55 lines (46 loc) · 928 Bytes
/
sftp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package sshpool
import (
"fmt"
"time"
"github.com/pkg/sftp"
)
type SFTPSession struct {
*sftp.Client
pool *Pool
client *client
host string
sessionid int
}
func (s *SFTPSession) String() string {
return fmt.Sprintf("sftp session %d host %s", s.sessionid, s.host)
}
func (p *Pool) GetSFTP(host string) (*SFTPSession, error) {
client, sessionid, err := p.get_client(host)
if err != nil {
return nil, err
}
s, err := sftp.NewClient(client.Client)
if err != nil {
_ = <-client.sessions
return nil, err
}
session := &SFTPSession{
Client: s,
sessionid: sessionid,
pool: p,
host: host,
client: client,
}
return session, nil
}
func (s *SFTPSession) Put() {
go func() {
if s.pool.poolconfig.SessionCloseDelay == 0 {
time.Sleep(DefaultSessionCloseDelay)
} else {
time.Sleep(s.pool.poolconfig.SessionCloseDelay)
}
_ = <-s.client.sessions
}()
return
}