aboutsummaryrefslogtreecommitdiff
path: root/pipeln.go
diff options
context:
space:
mode:
authorGrégoire Duchêne <gduchene@awhk.org>2021-03-16 15:16:03 +0000
committerGrégoire Duchêne <gduchene@awhk.org>2021-03-16 15:16:03 +0000
commitf06b2f85e8b1158bd63c7ed4d918770626d99545 (patch)
tree5c3623e265e0ebc21cacc890e478f8868c63f86e /pipeln.go
parentae2def146399745a8dba5113d5e1b57292a258e8 (diff)
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.
Diffstat (limited to 'pipeln.go')
-rw-r--r--pipeln.go19
1 files changed, 10 insertions, 9 deletions
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}
}