Skip to content

Commit

Permalink
Use channel to implement a simple way to wait
Browse files Browse the repository at this point in the history
  • Loading branch information
wutz committed Dec 5, 2018
1 parent 3a53acc commit 5cd7f32
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
3 changes: 1 addition & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"io"
"os"
"path"
"sync"
"sync/atomic"
"syscall"
"time"
Expand Down Expand Up @@ -123,7 +122,7 @@ func NewClientPipe(rd io.Reader, wr io.WriteCloser, opts ...ClientOption) (*Clie
WriteCloser: wr,
},
inflight: make(map[uint32]chan<- result),
errCond: sync.NewCond(new(sync.Mutex)),
closed: make(chan struct{}),
},
maxPacket: 1 << 15,
maxConcurrentRequests: 64,
Expand Down
16 changes: 5 additions & 11 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,15 @@ type clientConn struct {
sync.Mutex // protects inflight
inflight map[uint32]chan<- result // outstanding requests

errCond *sync.Cond
err error
closed chan struct{}
err error
}

// Wait blocks until the conn has shut down, and return the error
// causing the shutdown. It can be called concurrently from multiple
// goroutines.
func (c *clientConn) Wait() error {
c.errCond.L.Lock()
defer c.errCond.L.Unlock()
for c.err == nil {
c.errCond.Wait()
}
<-c.closed
return c.err
}

Expand All @@ -64,10 +60,6 @@ func (c *clientConn) loop() {
err := c.recv()
if err != nil {
c.broadcastErr(err)
c.errCond.L.Lock()
c.err = err
c.errCond.Broadcast()
c.errCond.L.Unlock()
}
}

Expand Down Expand Up @@ -141,6 +133,8 @@ func (c *clientConn) broadcastErr(err error) {
for _, ch := range listeners {
ch <- result{err: err}
}
c.err = err
close(c.closed)
}

type serverConn struct {
Expand Down

0 comments on commit 5cd7f32

Please sign in to comment.