added optional host to healthcheck endpoint

This commit is contained in:
paspo
2026-06-21 14:32:53 +02:00
parent 0332733982
commit a21aa69bc3
2 changed files with 17 additions and 9 deletions
+8 -5
View File
@@ -25,15 +25,17 @@ import (
) )
const ( const (
DefaultHost = "127.0.0.1"
DefaultPort = "51000" DefaultPort = "51000"
DefaultTimeout = 3 * time.Second DefaultTimeout = 3 * time.Second
) )
func StartListener(port string) { func StartListener(host, port string) {
addr := host + ":" + port
go func() { go func() {
l, err := net.Listen("tcp", "127.0.0.1:"+port) l, err := net.Listen("tcp", addr)
if err != nil { 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) os.Exit(1)
} }
defer l.Close() 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 timeout := DefaultTimeout
conn, err := net.DialTimeout("tcp", "127.0.0.1:"+port, timeout) conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil { if err != nil {
return false return false
} }
+9 -4
View File
@@ -146,8 +146,9 @@ func main() {
maxMindDbFileName := flag.String("max_mind_db", "", "Path to the MaxMind DB file.") 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.") 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.") 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") healthcheckHost := flag.String("healthcheck_host", health.DefaultHost, "The address for container healthcheck")
healthcheck := flag.Bool("healthcheck", false, "Dial healthcheck_port on 127.0.0.1 and exit 0 if reachable (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() { flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %v \n", os.Args[0]) fmt.Fprintf(flag.CommandLine.Output(), "Usage of %v \n", os.Args[0])
@@ -155,8 +156,12 @@ func main() {
} }
flag.Parse() flag.Parse()
if *connType == "tcp6" && *healthcheckHost == "0.0.0.0" {
*healthcheckHost = "[::]"
}
if *healthcheck { if *healthcheck {
if !health.Probe(*healthcheckPort) { if !health.Probe(*healthcheckHost, *healthcheckPort) {
os.Exit(1) os.Exit(1)
} }
os.Exit(0) os.Exit(0)
@@ -193,7 +198,7 @@ func main() {
if len(connPorts) == 0 { if len(connPorts) == 0 {
connPorts = append(connPorts, defaultPort) connPorts = append(connPorts, defaultPort)
} }
health.StartListener(*healthcheckPort) health.StartListener(*healthcheckHost, *healthcheckPort)
for _, connPort := range connPorts { for _, connPort := range connPorts {
startAccepting(*maxClients, *connType, *connHost, connPort, interval, clients, records, *proxyProtocolEnabled, *proxyProtocolReadHeaderTimeout) startAccepting(*maxClients, *connType, *connHost, connPort, interval, clients, records, *proxyProtocolEnabled, *proxyProtocolReadHeaderTimeout)
} }