diff --git a/staging/src/slime.io/slime/modules/lazyload/cmd/proxy/main.go b/staging/src/slime.io/slime/modules/lazyload/cmd/proxy/main.go index 01d9262f..6040f2ff 100644 --- a/staging/src/slime.io/slime/modules/lazyload/cmd/proxy/main.go +++ b/staging/src/slime.io/slime/modules/lazyload/cmd/proxy/main.go @@ -20,6 +20,9 @@ const ( ) func main() { + // indicate whether probe port is same with one of wormhole ports + dupPort := false + // set log config logLevel := os.Getenv(EnvLogLevel) if logLevel == "" { @@ -34,26 +37,17 @@ func main() { TimestampFormat: time.RFC3339, }) - // start health check server - probePort := os.Getenv(EnvProbePort) - go func() { - handler := &proxy.HealthzProxy{} - log.Println("Starting health check on", ":" + probePort) - if err := http.ListenAndServe(":" + probePort, handler); err != nil { - log.Fatal("ListenAndServe:", err) - } - }() + probePort, err := strconv.Atoi(os.Getenv(EnvProbePort)) + if err != nil { + log.Errorf("wrong probe port value %s", os.Getenv(EnvProbePort)) + os.Exit(1) + } // start multi ports defined in WormholePorts wormholePorts := os.Getenv(EnvWormholePorts) wormholePortsArr := strings.Split(wormholePorts, ",") var whPorts []int for _, whp := range wormholePortsArr { - if whp == probePort { - log.Errorf("wormholePort can not be %s, which is reserved for health check", probePort) - os.Exit(1) - } - p, err := strconv.Atoi(whp) if err != nil { log.Errorf("wrong wormholePort value %s", whp) @@ -66,6 +60,11 @@ func main() { for _, whPort := range whPorts { wg.Add(1) handler := &proxy.Proxy{WormholePort: whPort} + if whPort == probePort { + log.Println("Starting health check on", ":"+os.Getenv(EnvProbePort)) + handler.IsProbe = true + dupPort = true + } go func(whPort int) { log.Println("Starting proxy on", "0.0.0.0"+":"+strconv.Itoa(whPort)) if err := http.ListenAndServe("0.0.0.0"+":"+strconv.Itoa(whPort), handler); err != nil { @@ -75,6 +74,16 @@ func main() { }(whPort) } + if !dupPort { + go func() { + handler := &proxy.HealthzProxy{} + log.Println("Starting health check on", ":"+os.Getenv(EnvProbePort)) + if err := http.ListenAndServe(":"+os.Getenv(EnvProbePort), handler); err != nil { + log.Fatal("ListenAndServe:", err) + } + }() + } + wg.Wait() log.Infof("All servers exited.") } diff --git a/staging/src/slime.io/slime/modules/lazyload/pkg/proxy/http.go b/staging/src/slime.io/slime/modules/lazyload/pkg/proxy/http.go index 63b21d46..eeed6998 100644 --- a/staging/src/slime.io/slime/modules/lazyload/pkg/proxy/http.go +++ b/staging/src/slime.io/slime/modules/lazyload/pkg/proxy/http.go @@ -30,9 +30,18 @@ func (p *HealthzProxy) ServeHTTP(w http.ResponseWriter, req *http.Request) { type Proxy struct { WormholePort int + IsProbe bool } func (p *Proxy) ServeHTTP(w http.ResponseWriter, req *http.Request) { + if p.IsProbe { + // health check + if req.RequestURI == "/healthz/live" || req.RequestURI == "/healthz/ready" { + w.Write([]byte("Healthy!")) + return + } + } + var ( reqCtx = req.Context() reqHost = req.Host