Skip to content

Commit

Permalink
Use per-instance unix domain socket path (#7152)
Browse files Browse the repository at this point in the history
  • Loading branch information
foto-andreas committed Apr 1, 2023
1 parent 58f720b commit b093c5a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
7 changes: 6 additions & 1 deletion cmd/health.go
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Expand Up @@ -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()))
}
20 changes: 14 additions & 6 deletions server/uds.go
Expand Up @@ -3,6 +3,7 @@
package server

import (
"fmt"
"net"
"net/http"
"os"
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -40,6 +48,6 @@ func HealthListener(site site.API) {

shutdown.Register(func() {
_ = l.Close()
removeIfExists(SocketPath) // cleanup
removeIfExists(socket) // cleanup
})
}

0 comments on commit b093c5a

Please sign in to comment.