From a21aa69bc34948e4790502f21733e9d537edadf0 Mon Sep 17 00:00:00 2001 From: paspo Date: Sun, 21 Jun 2026 14:32:53 +0200 Subject: [PATCH] added optional host to healthcheck endpoint --- health/health.go | 13 ++++++++----- main.go | 13 +++++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/health/health.go b/health/health.go index f29bf95..00c64c1 100644 --- a/health/health.go +++ b/health/health.go @@ -25,15 +25,17 @@ import ( ) const ( + DefaultHost = "127.0.0.1" DefaultPort = "51000" DefaultTimeout = 3 * time.Second ) -func StartListener(port string) { +func StartListener(host, port string) { + addr := host + ":" + port go func() { - l, err := net.Listen("tcp", "127.0.0.1:"+port) + l, err := net.Listen("tcp", addr) if err != nil { - glog.Errorf("Error listening for healthcheck on 127.0.0.1:%v: %v", port, err) + glog.Errorf("Error listening for healthcheck on %v: %v", addr, err) os.Exit(1) } defer l.Close() @@ -48,9 +50,10 @@ func StartListener(port string) { }() } -func Probe(port string) bool { +func Probe(host, port string) bool { + addr := host + ":" + port timeout := DefaultTimeout - conn, err := net.DialTimeout("tcp", "127.0.0.1:"+port, timeout) + conn, err := net.DialTimeout("tcp", addr, timeout) if err != nil { return false } diff --git a/main.go b/main.go index d09b8f9..89b0447 100644 --- a/main.go +++ b/main.go @@ -146,8 +146,9 @@ func main() { maxMindDbFileName := flag.String("max_mind_db", "", "Path to the MaxMind DB file.") proxyProtocolEnabled := flag.Bool("proxy_protocol_enabled", false, "Enable PROXY protocol support. This causes the server to expect PROXY protocol headers on incoming connections.") proxyProtocolReadHeaderTimeout := flag.Int("proxy_protocol_read_header_timeout_ms", 200, "Timeout for reading the PROXY protocol header in milliseconds. If the connection does not send a valid PROXY protocol header in this time, the header is ignored.") - healthcheckPort := flag.String("healthcheck_port", health.DefaultPort, "TCP port for container healthcheck; accepts connectionsand closes without logging or metrics") - healthcheck := flag.Bool("healthcheck", false, "Dial healthcheck_port on 127.0.0.1 and exit 0 if reachable (for container healthcheck)") + healthcheckHost := flag.String("healthcheck_host", health.DefaultHost, "The address for container healthcheck") + healthcheckPort := flag.String("healthcheck_port", health.DefaultPort, "TCP port for container healthcheck; accepts connection and closes without logging or updating metrics") + healthcheck := flag.Bool("healthcheck", false, "Dial healthcheck_host:healthcheck_port and exit 0 if reachable (for container healthcheck)") flag.Usage = func() { fmt.Fprintf(flag.CommandLine.Output(), "Usage of %v \n", os.Args[0]) @@ -155,8 +156,12 @@ func main() { } flag.Parse() + if *connType == "tcp6" && *healthcheckHost == "0.0.0.0" { + *healthcheckHost = "[::]" + } + if *healthcheck { - if !health.Probe(*healthcheckPort) { + if !health.Probe(*healthcheckHost, *healthcheckPort) { os.Exit(1) } os.Exit(0) @@ -193,7 +198,7 @@ func main() { if len(connPorts) == 0 { connPorts = append(connPorts, defaultPort) } - health.StartListener(*healthcheckPort) + health.StartListener(*healthcheckHost, *healthcheckPort) for _, connPort := range connPorts { startAccepting(*maxClients, *connType, *connHost, connPort, interval, clients, records, *proxyProtocolEnabled, *proxyProtocolReadHeaderTimeout) }