@@ -19,11 +19,14 @@ import (
19
19
)
20
20
21
21
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
27
30
}
28
31
29
32
// HandlerOption is a function that configures the handler.
@@ -50,6 +53,27 @@ func WithUpstreamTLSOptions(opts *tls.UpstreamOptions) HandlerOption {
50
53
}
51
54
}
52
55
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
+
53
77
// WithReadMiddleware adds a middleware for read operations.
54
78
func WithReadMiddleware (m func (http.Handler ) http.Handler ) HandlerOption {
55
79
return func (o * handlerOptions ) {
@@ -67,7 +91,10 @@ func WithWriteMiddleware(m func(http.Handler) http.Handler) HandlerOption {
67
91
// NewHandler creates a new handler for the probes API.
68
92
func NewHandler (downstream * url.URL , opts ... HandlerOption ) (http.Handler , error ) {
69
93
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 ,
71
98
}
72
99
for _ , o := range opts {
73
100
o (options )
@@ -79,10 +106,10 @@ func NewHandler(downstream *url.URL, opts ...HandlerOption) (http.Handler, error
79
106
proxy .Transport = & http.Transport {
80
107
Proxy : http .ProxyFromEnvironment ,
81
108
DialContext : (& net.Dialer {
82
- Timeout : 30 * time . Second ,
83
- KeepAlive : 30 * time . Second ,
109
+ Timeout : options . dialTimeout ,
110
+ KeepAlive : options . keepAliveTimeout ,
84
111
}).DialContext ,
85
- TLSHandshakeTimeout : 10 * time . Second ,
112
+ TLSHandshakeTimeout : options . tlsHandshakeTimeout ,
86
113
TLSClientConfig : options .tlsOptions .NewClientConfig (),
87
114
}
88
115
0 commit comments