Repository navigation
Expand file tree
/
Copy pathexample_reconnect_test.go
More file actions
112 lines (94 loc) · 2.42 KB
/
Copy pathexample_reconnect_test.go
File metadata and controls
112 lines (94 loc) · 2.42 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package sftp_test
import (
"log"
"sync"
"time"
"github.com/pkg/sftp"
"golang.org/x/crypto/ssh"
)
// reconnectingClient hands out a connected *sftp.Client, and connects again
// whenever the previous connection has gone away.
// It only connects when a client is asked for,
// so nothing reconnects while nobody needs the connection.
type reconnectingClient struct {
addr string
config *ssh.ClientConfig
mu sync.Mutex
client *sftp.Client
done chan struct{} // closed once client has disconnected
}
// Client returns a connected *sftp.Client, connecting first if necessary.
// Cached sessions are checked before reuse.
// Do not hold on to the returned client; ask for it again each time it is needed.
func (rc *reconnectingClient) Client() (*sftp.Client, error) {
rc.mu.Lock()
defer rc.mu.Unlock()
if rc.client != nil {
select {
case <-rc.done:
// disconnected, so connect again below
default:
// Wait's notification may still be pending after the session ends.
if _, err := rc.client.RealPath("."); err == nil {
return rc.client, nil
}
rc.client.Close()
}
rc.client = nil
}
conn, err := ssh.Dial("tcp", rc.addr, rc.config)
if err != nil {
return nil, err
}
client, err := sftp.NewClient(conn)
if err != nil {
conn.Close()
return nil, err
}
done := make(chan struct{})
go func() {
// Wait returns once the SFTP session has ended, for whatever reason.
// Closing the sftp.Client does not close the ssh.Client, so do that here.
err := client.Wait()
conn.Close()
close(done)
log.Print("sftp: disconnected: ", err)
}()
rc.client, rc.done = client, done
return client, nil
}
// Close closes the current connection, if there is one.
func (rc *reconnectingClient) Close() error {
rc.mu.Lock()
defer rc.mu.Unlock()
if rc.client == nil {
return nil
}
err := rc.client.Close()
<-rc.done
rc.client = nil
return err
}
func Example_reconnect() {
var config *ssh.ClientConfig
rc := &reconnectingClient{
addr: "example.com:22",
config: config,
}
defer rc.Close()
for range time.Tick(time.Minute) {
client, err := rc.Client()
if err != nil {
log.Print(err)
continue
}
// If the connection drops while a request is in flight, the request fails
// with sftp.ErrSSHFxConnectionLost, and the next call to rc.Client() connects again.
fi, err := client.Stat("/upload/ready")
if err != nil {
log.Print(err)
continue
}
log.Print(fi.Name(), " is ready")
}
}