From b093c5acc3e418a1cfe023b483be2a5abb2222b3 Mon Sep 17 00:00:00 2001 From: Andreas Schrell Date: Sat, 1 Apr 2023 16:43:15 +0200 Subject: [PATCH] Use per-instance unix domain socket path (#7152) --- cmd/health.go | 7 ++++++- cmd/root.go | 2 +- server/uds.go | 20 ++++++++++++++------ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/cmd/health.go b/cmd/health.go index 8f1ba69c92..35d0022bdd 100644 --- a/cmd/health.go +++ b/cmd/health.go @@ -27,13 +27,18 @@ func init() { } func runHealth(cmd *cobra.Command, args []string) { + // load config + if err := loadConfigFile(&conf); err != nil { + log.FATAL.Fatal(err) + } + u := &httpunix.Transport{ DialTimeout: 100 * time.Millisecond, RequestTimeout: 1 * time.Second, ResponseHeaderTimeout: 1 * time.Second, } - u.RegisterLocation(serviceName, server.SocketPath) + u.RegisterLocation(serviceName, server.SocketPath(conf.Network.Port)) client := http.Client{ Transport: u, diff --git a/cmd/root.go b/cmd/root.go index 1d9ad570d4..8f962eb0e3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -296,7 +296,7 @@ func runRoot(cmd *cobra.Command, args []string) { } // uds health check listener - go server.HealthListener(site) + go server.HealthListener(site, conf.Network.Port) log.FATAL.Println(wrapErrors(httpd.ListenAndServe())) } diff --git a/server/uds.go b/server/uds.go index 0dbdc7f42b..2be625c570 100644 --- a/server/uds.go +++ b/server/uds.go @@ -3,6 +3,7 @@ package server import ( + "fmt" "net" "net/http" "os" @@ -11,8 +12,13 @@ import ( "github.com/evcc-io/evcc/core/site" ) -// SocketPath is the unix domain socket path -const SocketPath = "/tmp/evcc" +// socketPath is the unix domain socket path +const socketPath = "/tmp/evcc-%d" + +// SocketPath returns the unix domain socket path for a given port +func SocketPath(port int) string { + return fmt.Sprintf(socketPath, port) +} // removeIfExists deletes file if it exists or fails func removeIfExists(file string) { @@ -24,10 +30,12 @@ func removeIfExists(file string) { } // HealthListener attaches listener to unix domain socket and runs listener -func HealthListener(site site.API) { - removeIfExists(SocketPath) +func HealthListener(site site.API, port int) { + socket := SocketPath(port) + + removeIfExists(socket) - l, err := net.Listen("unix", SocketPath) + l, err := net.Listen("unix", socket) if err != nil { log.FATAL.Fatal(err) } @@ -40,6 +48,6 @@ func HealthListener(site site.API) { shutdown.Register(func() { _ = l.Close() - removeIfExists(SocketPath) // cleanup + removeIfExists(socket) // cleanup }) }