@@ -19,11 +19,14 @@ import (
1919)
2020
2121type 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.
5478func 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.
6892func 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
0 commit comments