@@ -19,6 +19,7 @@ package main
1919import (
2020 "context"
2121 "flag"
22+ "fmt"
2223 "net"
2324 "net/http"
2425 "os"
@@ -48,6 +49,7 @@ func main() {
4849 userContainerPort int
4950 maxConcurrency int
5051 maxQueueLength int
52+ hasTCPProbe bool
5153 clusterConfigPath string
5254 )
5355
@@ -56,6 +58,7 @@ func main() {
5658 flag .IntVar (& userContainerPort , "user-port" , 8080 , "port where the proxy will redirect to the traffic to" )
5759 flag .IntVar (& maxConcurrency , "max-concurrency" , 0 , "max concurrency allowed for user container" )
5860 flag .IntVar (& maxQueueLength , "max-queue-length" , 0 , "max request queue length for user container" )
61+ flag .BoolVar (& hasTCPProbe , "has-tcp-probe" , false , "tcp probe to the user-provided container port" )
5962 flag .StringVar (& clusterConfigPath , "cluster-config" , "" , "cluster config path" )
6063 flag .Parse ()
6164
@@ -142,7 +145,7 @@ func main() {
142145
143146 adminHandler := http .NewServeMux ()
144147 adminHandler .Handle ("/metrics" , promStats )
145- adminHandler .Handle ("/healthz" , readinessTCPHandler (userContainerPort , log ))
148+ adminHandler .Handle ("/healthz" , readinessTCPHandler (userContainerPort , hasTCPProbe , log ))
146149
147150 servers := map [string ]* http.Server {
148151 "proxy" : {
@@ -201,19 +204,22 @@ func exit(log *zap.SugaredLogger, err error, wrapStrs ...string) {
201204 os .Exit (1 )
202205}
203206
204- func readinessTCPHandler (port int , logger * zap.SugaredLogger ) http.HandlerFunc {
207+ func readinessTCPHandler (port int , enableTCPProbe bool , logger * zap.SugaredLogger ) http.HandlerFunc {
205208 return func (w http.ResponseWriter , r * http.Request ) {
206- timeout := time .Duration (1 ) * time .Second
207- address := net .JoinHostPort ("localhost" , strconv .FormatInt (int64 (port ), 10 ))
208-
209- conn , err := net .DialTimeout ("tcp" , address , timeout )
210- if err != nil {
211- logger .Warn (errors .Wrap (err , "TCP probe to user-provided container port failed" ))
212- w .WriteHeader (http .StatusInternalServerError )
213- _ , _ = w .Write ([]byte ("unhealthy" ))
214- return
209+ if enableTCPProbe {
210+ ctx := r .Context ()
211+ address := net .JoinHostPort ("localhost" , fmt .Sprintf ("%d" , port ))
212+
213+ var d net.Dialer
214+ conn , err := d .DialContext (ctx , "tcp" , address )
215+ if err != nil {
216+ logger .Warn (errors .Wrap (err , "TCP probe to user-provided container port failed" ))
217+ w .WriteHeader (http .StatusInternalServerError )
218+ _ , _ = w .Write ([]byte ("unhealthy" ))
219+ return
220+ }
221+ _ = conn .Close ()
215222 }
216- _ = conn .Close ()
217223
218224 w .WriteHeader (http .StatusOK )
219225 _ , _ = w .Write ([]byte ("healthy" ))
0 commit comments