From f06b2f85e8b1158bd63c7ed4d918770626d99545 Mon Sep 17 00:00:00 2001 From: GrĂ©goire DuchĂȘne Date: Tue, 16 Mar 2021 15:16:03 +0000 Subject: PipeListenerDialer.Close should be called once PipeListenerDialer.Close will return unix.EINVAL if it is called more than once. This is to emulate the behavior of other standard listeners. Also: * PipeListenerDialer.Accept will return (nil, unix.EINVAL) if the listener is closed. This roughly matches with behavior described in accept(2). * PipeListenerDialer.Dial will also return (nil, unix.EINVAL) if the address passed does not match the one passed to New. It will also return (nil, unix.ECONNREFUSED) if the listener is closed. This roughly matches the behavior described in connect(2) and unix(7). * The custom errors ErrBadAddress and ErrClosed are removed. --- pipeln.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'pipeln.go') diff --git a/pipeln.go b/pipeln.go index c836f28..29c6523 100644 --- a/pipeln.go +++ b/pipeln.go @@ -4,13 +4,9 @@ package pipeln import ( "context" - "errors" "net" -) -var ( - ErrBadAddress = errors.New("bad address") - ErrClosed = errors.New("closed listener") + "golang.org/x/sys/unix" ) type addr struct { @@ -34,6 +30,7 @@ type PipeListenerDialer struct { addr string conns chan net.Conn done chan struct{} + ok bool } var _ net.Listener = &PipeListenerDialer{} @@ -43,7 +40,7 @@ func (ln *PipeListenerDialer) Accept() (net.Conn, error) { case conn := <-ln.conns: return conn, nil case <-ln.done: - return nil, ErrClosed + return nil, unix.EINVAL } } @@ -52,20 +49,24 @@ func (ln *PipeListenerDialer) Addr() net.Addr { } func (ln *PipeListenerDialer) Close() error { + if !ln.ok { + return unix.EINVAL + } close(ln.done) + ln.ok = false return nil } func (ln *PipeListenerDialer) Dial(_, addr string) (net.Conn, error) { if addr != ln.addr { - return nil, ErrBadAddress + return nil, unix.EINVAL } s, c := net.Pipe() select { case ln.conns <- s: return c, nil case <-ln.done: - return nil, ErrClosed + return nil, unix.ECONNREFUSED } } @@ -78,5 +79,5 @@ func (ln *PipeListenerDialer) DialContextAddr(_ context.Context, addr string) (n } func New(addr string) *PipeListenerDialer { - return &PipeListenerDialer{addr, make(chan net.Conn), make(chan struct{})} + return &PipeListenerDialer{addr, make(chan net.Conn), make(chan struct{}), true} } -- cgit v1.2.3-70-g09d2