Skip to content

Commit

Permalink
Fix bufconn.Close to not be blocking. (#1377)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfawley authored Jul 20, 2017
1 parent 98eab9b commit 2bb3182
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
38 changes: 20 additions & 18 deletions test/bufconn/bufconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,39 +31,42 @@ import (
// Listener implements a net.Listener that creates local, buffered net.Conns
// via its Accept and Dial method.
type Listener struct {
mu sync.Mutex
sz int
ch chan net.Conn
closed bool
mu sync.Mutex
sz int
ch chan net.Conn
done chan struct{}
}

var errClosed = fmt.Errorf("Closed")

// Listen returns a Listener that can only be contacted by its own Dialers and
// creates buffered connections between the two.
func Listen(sz int) *Listener {
return &Listener{sz: sz, ch: make(chan net.Conn)}
return &Listener{sz: sz, ch: make(chan net.Conn), done: make(chan struct{})}
}

// Accept blocks until Dial is called, then returns a net.Conn for the server
// half of the connection.
func (l *Listener) Accept() (net.Conn, error) {
c := <-l.ch
if c == nil {
select {
case <-l.done:
return nil, errClosed
case c := <-l.ch:
return c, nil
}
return c, nil
}

// Close stops the listener.
func (l *Listener) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
if l.closed {
return nil
select {
case <-l.done:
// Already closed.
break
default:
close(l.done)
}
l.closed = true
close(l.ch)
return nil
}

Expand All @@ -74,14 +77,13 @@ func (l *Listener) Addr() net.Addr { return addr{} }
// providing it the server half of the connection, and returns the client half
// of the connection.
func (l *Listener) Dial() (net.Conn, error) {
l.mu.Lock()
defer l.mu.Unlock()
if l.closed {
p1, p2 := newPipe(l.sz), newPipe(l.sz)
select {
case <-l.done:
return nil, errClosed
case l.ch <- &conn{p1, p2}:
return &conn{p2, p1}, nil
}
p1, p2 := newPipe(l.sz), newPipe(l.sz)
l.ch <- &conn{p1, p2}
return &conn{p2, p1}, nil
}

type pipe struct {
Expand Down
32 changes: 32 additions & 0 deletions test/bufconn/bufconn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,35 @@ func TestListener(t *testing.T) {
t.Fatalf(err.Error())
}
}

func TestCloseWhileDialing(t *testing.T) {
l := Listen(7)
var c net.Conn
var err error
done := make(chan struct{})
go func() {
c, err = l.Dial()
close(done)
}()
l.Close()
<-done
if c != nil || err != errClosed {
t.Fatalf("c, err = %v, %v; want nil, %v", c, err, errClosed)
}
}

func TestCloseWhileAccepting(t *testing.T) {
l := Listen(7)
var c net.Conn
var err error
done := make(chan struct{})
go func() {
c, err = l.Accept()
close(done)
}()
l.Close()
<-done
if c != nil || err != errClosed {
t.Fatalf("c, err = %v, %v; want nil, %v", c, err, errClosed)
}
}

0 comments on commit 2bb3182

Please sign in to comment.