From 50c81d78383376585e942578c2410d06803cb16e Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Mon, 18 Mar 2024 11:40:06 +0200 Subject: [PATCH] feat(tvix/nar-bridge): support listening on unix sockets This simply checks for the address to contain slashes, and if so, opens a unix socket, rather than a tcp one. We'll use this in //tvix/boot tests to simplify waiting for nar-bridge to be up. Change-Id: I7184f548d57142b1c5f698a1f0c30343489373a5 Reviewed-on: https://cl.tvl.fyi/c/depot/+/11184 Tested-by: BuildkiteCI Autosubmit: flokli Reviewed-by: Connor Brewster --- tvix/nar-bridge/pkg/http/server.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tvix/nar-bridge/pkg/http/server.go b/tvix/nar-bridge/pkg/http/server.go index aecbd54ae..fbcb20be1 100644 --- a/tvix/nar-bridge/pkg/http/server.go +++ b/tvix/nar-bridge/pkg/http/server.go @@ -3,7 +3,9 @@ package http import ( "context" "fmt" + "net" "net/http" + "strings" "sync" "time" @@ -94,12 +96,24 @@ func (s *Server) Shutdown(ctx context.Context) error { // shutdown, after which it'll return ErrServerClosed. func (s *Server) ListenAndServe(addr string) error { s.srv = &http.Server{ - Addr: addr, Handler: s.handler, ReadTimeout: 500 * time.Second, WriteTimeout: 500 * time.Second, IdleTimeout: 500 * time.Second, } - return s.srv.ListenAndServe() + var listener net.Listener + var err error + + // check addr. If it contains slashes, assume it's a unix domain socket. + if strings.Contains(addr, "/") { + listener, err = net.Listen("unix", addr) + } else { + listener, err = net.Listen("tcp", addr) + } + if err != nil { + return fmt.Errorf("unable to listen on %v: %w", addr, err) + } + + return s.srv.Serve(listener) }