mirror of
https://github.com/shizunge/endlessh-go.git
synced 2026-08-07 07:15:02 +00:00
added optional host to healthcheck endpoint
This commit is contained in:
+8
-5
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user