From 95b759ad164cb092614aa063af44bf32b0b2b691 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Tue, 18 Mar 2014 17:06:31 -0700 Subject: [PATCH] Beam: change the prototype of SendPipe() to return a *net.UnixSocket Docker-DCO-1.1-Signed-off-by: Solomon Hykes (github: shykes) --- beam/service.go | 6 +----- beam/unix.go | 17 +++++++++++++---- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/beam/service.go b/beam/service.go index 69e82e7..9a21e35 100644 --- a/beam/service.go +++ b/beam/service.go @@ -17,11 +17,7 @@ import ( // not point to a connection, that message will be skipped. // func Listen(conn *net.UnixConn, name string) (net.Listener, error) { - fEndpoint, err := SendPipe(conn, []byte(name)) - if err != nil { - return nil, err - } - endpoint, err := FdConn(int(fEndpoint.Fd())) + endpoint, err := SendPipe(conn, []byte(name)) if err != nil { return nil, err } diff --git a/beam/unix.go b/beam/unix.go index cab8ee2..15e8ff0 100644 --- a/beam/unix.go +++ b/beam/unix.go @@ -78,16 +78,25 @@ func Receive(conn *net.UnixConn) ([]byte, *os.File, error) { // allows for arbitrarily complex service discovery and retry logic to take place, // without complicating application code. // -func SendPipe(conn *net.UnixConn, data []byte) (*os.File, error) { +func SendPipe(conn *net.UnixConn, data []byte) (endpoint *net.UnixConn, err error) { local, remote, err := SocketPair() if err != nil { return nil, err } - if err := Send(conn, data, remote); err != nil { - remote.Close() + defer func() { + if err != nil { + local.Close() + remote.Close() + } + }() + endpoint, err = FdConn(int(local.Fd())) + if err != nil { return nil, err } - return local, nil + if err := Send(conn, data, remote); err != nil { + return nil, err + } + return endpoint, nil } func receiveUnix(conn *net.UnixConn) ([]byte, []int, error) {