diff --git a/go.mod b/go.mod index 6321114..1e82c0b 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/bytedance/sonic/loader v0.1.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect + github.com/coreos/go-systemd/v22 v22.6.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/gin-contrib/sse v0.1.0 // indirect diff --git a/go.sum b/go.sum index d1561ea..a7ee617 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ github.com/cloudwego/base64x v0.1.4 h1:jwCgWpFanWmN8xoIUHa2rtzmkd5J2plF/dnLS6Xd/ github.com/cloudwego/base64x v0.1.4/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w= github.com/cloudwego/iasm v0.2.0 h1:1KNIy1I1H9hNNFEEH3DVnI4UujN+1zjpuk6gwHLTssg= github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY= +github.com/coreos/go-systemd/v22 v22.6.0 h1:aGVa/v8B7hpb0TKl0MWoAavPDmHvobFe5R5zn0bCJWo= +github.com/coreos/go-systemd/v22 v22.6.0/go.mod h1:iG+pp635Fo7ZmV/j14KUcmEyWF+0X7Lua8rrTWzYgWU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/llama-swap.go b/llama-swap.go index 4f8febb..4fbce90 100644 --- a/llama-swap.go +++ b/llama-swap.go @@ -10,12 +10,11 @@ import ( "os" "os/signal" "path/filepath" - "strconv" "syscall" "time" - "github.com/fsnotify/fsnotify" "github.com/gin-gonic/gin" + "github.com/coreos/go-systemd/v22/activation" "github.com/mostlygeek/llama-swap/event" "github.com/mostlygeek/llama-swap/proxy" "github.com/mostlygeek/llama-swap/proxy/config" @@ -68,30 +67,17 @@ func main() { } // Check for systemd socket activation var listener net.Listener - listenFdsStr := os.Getenv("LISTEN_FDS") - if listenFdsStr != "" { - listenFds, err := strconv.Atoi(listenFdsStr) - if err != nil { - log.Fatal("Failed to parse LISTEN_FDS:", err) + files, err := activation.Listeners() + if err != nil { + log.Fatalf("Systemd activation error: %v", err) + } + if len(files) > 0 { + if len(files) > 1 { + log.Fatalf("Expected a single activated socket, got %d", len(files)) } - if listenFds > 0 { - // The first socket is on FD 3 - fd := 3 - f := os.NewFile(uintptr(fd), "systemd socket") - if f == nil { - log.Fatal("Failed to get file descriptor") - } - listener, err = net.FileListener(f) - if err != nil { - log.Fatal("Failed to create listener:", err) - } - // The listener now owns the file, so we don't close it here. - } else { - log.Fatal("LISTEN_FDS is set but no sockets are passed") - } - } else { - - + listener = files[0] + log.Printf("Using systemd socket %s", listener.Addr()) + } else { // Set default ports. if *listenStr == "" { defaultPort := ":8080" @@ -105,6 +91,8 @@ func main() { } } } + defer listener.Close() + // Setup channels for server management exitChan := make(chan struct{}) sigChan := make(chan os.Signal, 1)