8000 Use channel to implement a simple way to wait · etherscan-io/sftp@5cd7f32 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5cd7f32

Browse files
committed
Use channel to implement a simple way to wait
1 parent 3a53acc commit 5cd7f32

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

client.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"io"
77
"os"
88
"path"
9-
"sync"
109
"sync/atomic"
1110
"syscall"
1211
"time"
@@ -123,7 +122,7 @@ func NewClientPipe(rd io.Reader, wr io.WriteCloser, opts ...ClientOption) (*Clie
123122
WriteCloser: wr,
124123
},
125124
inflight: make(map[uint32]chan<- result),
126-
errCond: sync.NewCond(new(sync.Mutex)),
125+
closed: make(chan struct{}),
127126
},
128127
maxPacket: 1 << 15,
129128
maxConcurrentRequests: 64,

conn.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,15 @@ type clientConn struct {
3737
sync.Mutex // protects inflight
3838
inflight map[uint32]chan<- result // outstanding requests
3939

40-
errCond *sync.Cond
41-
err error
40+
closed chan struct{}
41+
err error
4242
}
4343

4444
// Wait blocks until the conn has shut down, and return the error
4545
// causing the shutdown. It can be called concurrently from multiple
4646
// goroutines.
4747
func (c *clientConn) Wait() error {
48-
c.errCond.L.Lock()
49-
defer c.errCond.L.Unlock()
50-
for c.err == nil {
51-
c.errCond.Wait()
52-
}
48+
<-c.closed
5349
return c.err
5450
}
5551

@@ -64,10 +60,6 @@ func (c *clientConn) loop() {
6460
err := c.recv()
6561
if err != nil {
6662
c.broadcastErr(err)
67-
c.errCond.L.Lock()
68-
c.err = err
69-
c.errCond.Broadcast()
70-
c.errCond.L.Unlock()
7163
}
7264
}
7365

@@ -141,6 +133,8 @@ func (c *clientConn) broadcastErr(err error) {
141133
for _, ch := range listeners {
142134
ch <- result{err: err}
143135
}
136+
c.err = err
137+
close(c.closed)
144138
}
145139

146140
type serverConn struct {

0 commit comments

Comments
 (0)
0