@@ -19,6 +19,7 @@ package main
19
19
import (
20
20
"context"
21
21
"flag"
22
+ "fmt"
22
23
"net"
23
24
"net/http"
24
25
"os"
@@ -48,6 +49,7 @@ func main() {
48
49
userContainerPort int
49
50
maxConcurrency int
50
51
maxQueueLength int
52
+ hasTCPProbe bool
51
53
clusterConfigPath string
52
54
)
53
55
@@ -56,6 +58,7 @@ func main() {
56
58
flag .IntVar (& userContainerPort , "user-port" , 8080 , "port where the proxy will redirect to the traffic to" )
57
59
flag .IntVar (& maxConcurrency , "max-concurrency" , 0 , "max concurrency allowed for user container" )
58
60
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" )
59
62
flag .StringVar (& clusterConfigPath , "cluster-config" , "" , "cluster config path" )
60
63
flag .Parse ()
61
64
@@ -142,7 +145,7 @@ func main() {
142
145
143
146
adminHandler := http .NewServeMux ()
144
147
adminHandler .Handle ("/metrics" , promStats )
145
- adminHandler .Handle ("/healthz" , readinessTCPHandler (userContainerPort , log ))
148
+ adminHandler .Handle ("/healthz" , readinessTCPHandler (userContainerPort , hasTCPProbe , log ))
146
149
147
150
servers := map [string ]* http.Server {
148
151
"proxy" : {
@@ -201,19 +204,22 @@ func exit(log *zap.SugaredLogger, err error, wrapStrs ...string) {
201
204
os .Exit (1 )
202
205
}
203
206
204
- func readinessTCPHandler (port int , logger * zap.SugaredLogger ) http.HandlerFunc {
207
+ func readinessTCPHandler (port int , enableTCPProbe bool , logger * zap.SugaredLogger ) http.HandlerFunc {
205
208
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 ()
215
222
}
216
- _ = conn .Close ()
217
223
218
224
w .WriteHeader (http .StatusOK )
219
225
_ , _ = w .Write ([]byte ("healthy" ))
0 commit comments