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 <flokli@flokli.de>
Reviewed-by: Connor Brewster <cbrewster@hey.com>
This commit is contained in:
Florian Klink 2024-03-18 11:40:06 +02:00 committed by clbot
parent 70bbf23767
commit 50c81d7838

View file

@ -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)
}