Use per-instance unix domain socket path (#7152)

This commit is contained in:
Andreas Schrell 2023-04-01 16:43:15 +02:00 • committed by GitHub
parent 58f720b74c
commit b093c5acc3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 8 deletions

View file

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

View file

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

View file

@ -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)
l, err := net.Listen("unix", SocketPath)
removeIfExists(socket)
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
})
}