Skip to content

Commit a713b07

Browse files
authored
Merge pull request pkg#279 from wutzx/client-conn-wait
Add Wait method to detect underlying SFTP connection closed
2 parents c35a030 + 5cd7f32 commit a713b07

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

client.go

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func NewClientPipe(rd io.Reader, wr io.WriteCloser, opts ...ClientOption) (*Clie
122122
WriteCloser: wr,
123123
},
124124
inflight: make(map[uint32]chan<- result),
125+
closed: make(chan struct{}),
125126
},
126127
maxPacket: 1 << 15,
127128
maxConcurrentRequests: 64,

conn.go

+13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ type clientConn struct {
3636
wg sync.WaitGroup
3737
sync.Mutex // protects inflight
3838
inflight map[uint32]chan<- result // outstanding requests
39+
40+
closed chan struct{}
41+
err error
42+
}
43+
44+
// Wait blocks until the conn has shut down, and return the error
45+
// causing the shutdown. It can be called concurrently from multiple
46+
// goroutines.
47+
func (c *clientConn) Wait() error {
48+
<-c.closed
49+
return c.err
3950
}
4051

4152
// Close closes the SFTP session.
@@ -122,6 +133,8 @@ func (c *clientConn) broadcastErr(err error) {
122133
for _, ch := range listeners {
123134
ch <- result{err: err}
124135
}
136+
c.err = err
137+
close(c.closed)
125138
}
126139

127140
type serverConn struct {

0 commit comments

Comments
 (0)