Skip to content

Commit b7a0c04

Browse files
committed
make timeouts configurable
1 parent 1724263 commit b7a0c04

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

api/probes/v1/http.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ import (
1919
)
2020

2121
type handlerOptions struct {
22-
logger log.Logger
23-
tenantHeader string
24-
tlsOptions *tls.UpstreamOptions
25-
readMiddlewares []func(http.Handler) http.Handler
26-
writeMiddlewares []func(http.Handler) http.Handler
22+
logger log.Logger
23+
tenantHeader string
24+
tlsOptions *tls.UpstreamOptions
25+
dialTimeout time.Duration
26+
keepAliveTimeout time.Duration
27+
tlsHandshakeTimeout time.Duration
28+
readMiddlewares []func(http.Handler) http.Handler
29+
writeMiddlewares []func(http.Handler) http.Handler
2730
}
2831

2932
// HandlerOption is a function that configures the handler.
@@ -50,6 +53,27 @@ func WithUpstreamTLSOptions(opts *tls.UpstreamOptions) HandlerOption {
5053
}
5154
}
5255

56+
// WithDialTimeout sets the dial timeout for upstream connections.
57+
func WithDialTimeout(timeout time.Duration) HandlerOption {
58+
return func(o *handlerOptions) {
59+
o.dialTimeout = timeout
60+
}
61+
}
62+
63+
// WithKeepAliveTimeout sets the keep-alive timeout for upstream connections.
64+
func WithKeepAliveTimeout(timeout time.Duration) HandlerOption {
65+
return func(o *handlerOptions) {
66+
o.keepAliveTimeout = timeout
67+
}
68+
}
69+
70+
// WithTLSHandshakeTimeout sets the TLS handshake timeout for upstream connections.
71+
func WithTLSHandshakeTimeout(timeout time.Duration) HandlerOption {
72+
return func(o *handlerOptions) {
73+
o.tlsHandshakeTimeout = timeout
74+
}
75+
}
76+
5377
// WithReadMiddleware adds a middleware for read operations.
5478
func WithReadMiddleware(m func(http.Handler) http.Handler) HandlerOption {
5579
return func(o *handlerOptions) {
@@ -67,7 +91,10 @@ func WithWriteMiddleware(m func(http.Handler) http.Handler) HandlerOption {
6791
// NewHandler creates a new handler for the probes API.
6892
func NewHandler(downstream *url.URL, opts ...HandlerOption) (http.Handler, error) {
6993
options := &handlerOptions{
70-
logger: log.NewNopLogger(),
94+
logger: log.NewNopLogger(),
95+
dialTimeout: 30 * time.Second,
96+
keepAliveTimeout: 30 * time.Second,
97+
tlsHandshakeTimeout: 10 * time.Second,
7198
}
7299
for _, o := range opts {
73100
o(options)
@@ -79,10 +106,10 @@ func NewHandler(downstream *url.URL, opts ...HandlerOption) (http.Handler, error
79106
proxy.Transport = &http.Transport{
80107
Proxy: http.ProxyFromEnvironment,
81108
DialContext: (&net.Dialer{
82-
Timeout: 30 * time.Second,
83-
KeepAlive: 30 * time.Second,
109+
Timeout: options.dialTimeout,
110+
KeepAlive: options.keepAliveTimeout,
84111
}).DialContext,
85-
TLSHandshakeTimeout: 10 * time.Second,
112+
TLSHandshakeTimeout: options.tlsHandshakeTimeout,
86113
TLSClientConfig: options.tlsOptions.NewClientConfig(),
87114
}
88115

main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ type tracesConfig struct {
200200
type probesConfig struct {
201201
endpoint *url.URL
202202
upstreamWriteTimeout time.Duration
203+
dialTimeout time.Duration
204+
keepAliveTimeout time.Duration
205+
tlsHandshakeTimeout time.Duration
203206
upstreamCAFile string
204207
upstreamCertFile string
205208
upstreamKeyFile string
@@ -710,6 +713,9 @@ func main() {
710713
probesv1.WithLogger(logger),
711714
probesv1.WithUpstreamTLSOptions(probesUpstreamClientOptions),
712715
probesv1.WithTenantHeader(cfg.probes.tenantHeader),
716+
probesv1.WithDialTimeout(cfg.probes.dialTimeout),
717+
probesv1.WithKeepAliveTimeout(cfg.probes.keepAliveTimeout),
718+
probesv1.WithTLSHandshakeTimeout(cfg.probes.tlsHandshakeTimeout),
713719
probesv1.WithReadMiddleware(authentication.WithTenantMiddlewares(pm.Middlewares)),
714720
probesv1.WithReadMiddleware(rateLimitMiddleware),
715721
probesv1.WithReadMiddleware(authorization.WithAuthorizers(authorizers, rbac.Read, "probes")),
@@ -1240,6 +1246,12 @@ func parseFlags() (config, error) {
12401246
"Watch for certificate changes and reload")
12411247
flag.StringVar(&cfg.probes.tenantHeader, "probes.tenant-header", "X-Tenant",
12421248
"The name of the HTTP header containing the tenant ID to forward to the probes upstream.")
1249+
flag.DurationVar(&cfg.probes.dialTimeout, "probes.dial-timeout", 30*time.Second,
1250+
"The timeout for establishing connections to the probes upstream.")
1251+
flag.DurationVar(&cfg.probes.keepAliveTimeout, "probes.keep-alive-timeout", 30*time.Second,
1252+
"The keep-alive timeout for connections to the probes upstream.")
1253+
flag.DurationVar(&cfg.probes.tlsHandshakeTimeout, "probes.tls-handshake-timeout", 10*time.Second,
1254+
"The TLS handshake timeout for connections to the probes upstream.")
12431255
flag.StringVar(&cfg.tls.serverCertFile, "tls.server.cert-file", "",
12441256
"File containing the default x509 Certificate for HTTPS. Leave blank to disable TLS.")
12451257
flag.StringVar(&cfg.tls.serverKeyFile, "tls.server.key-file", "",

0 commit comments

Comments
 (0)