Corrected implementation using coreOS systemd library

This commit is contained in:
2025-11-20 00:18:07 +01:00
parent 86f433c0c2
commit 553d8659b7
3 changed files with 16 additions and 25 deletions

View File

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