about summary refs log tree commit diff
path: root/tvix/nar-bridge/pkg/http/server.go
diff options
context:
space:
mode:
authorFlorian Klink <flokli@flokli.de>2024-03-18T09·40+0200
committerclbot <clbot@tvl.fyi>2024-03-18T14·57+0000
commit50c81d78383376585e942578c2410d06803cb16e (patch)
tree93bf780b8f39436ec9bd34c41f122bd0af72aff2 /tvix/nar-bridge/pkg/http/server.go
parent70bbf23767754b057afbcd05d476be1bf8c23157 (diff)
feat(tvix/nar-bridge): support listening on unix sockets r/7721
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 <flokli@flokli.de>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
Diffstat (limited to 'tvix/nar-bridge/pkg/http/server.go')
-rw-r--r--tvix/nar-bridge/pkg/http/server.go18
1 files changed, 16 insertions, 2 deletions
diff --git a/tvix/nar-bridge/pkg/http/server.go b/tvix/nar-bridge/pkg/http/server.go
index aecbd54aec16..fbcb20be18b7 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)
 }