Skip to content

Commit 98fc178

Browse files
committed
feat: optional configuration to change response header timeout
Signed-off-by: Alano Terblanche <18033717+Benehiko@users.noreply.github.com>
1 parent c062c3f commit 98fc178

1 file changed

Lines changed: 34 additions & 5 deletions

File tree

client/client.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,46 @@ func WithDialContext(dialContext func(ctx context.Context, network, addr string)
5959
}
6060
}
6161

62+
// WithTimeout overrides the request timeout of the client.
63+
//
64+
// It is useful to set if there are hard-limits to when the client must wait
65+
// for the server to accept the request.
66+
//
67+
// A timout of 0 means no request timeout will be applied.
68+
// Negative durations are not allowed and will result in an error.
6269
func WithTimeout(timeout time.Duration) Option {
6370
return func(s *config) error {
71+
if timeout < 0 {
72+
return errors.New("request timeout duration cannot be negative")
73+
}
6474
s.requestTimeout = timeout
6575
return nil
6676
}
6777
}
6878

79+
// WithResponseTimeout overrides the response header timeout of the client.
80+
//
81+
// It is useful to set if there are long-lived user interactions required
82+
// when the Secrets Engine requests secrets from a plugin.
83+
//
84+
// A responseTimeout of 0 means no response header timeout will be applied.
85+
// Negative durations are not allowed and will result in an error.
86+
func WithResponseTimeout(responseTimeout time.Duration) Option {
87+
return func(s *config) error {
88+
if responseTimeout < 0 {
89+
return errors.New("response timeout duration cannot be negative")
90+
}
91+
s.responseTimeout = responseTimeout
92+
return nil
93+
}
94+
}
95+
6996
type dial func(ctx context.Context, network, addr string) (net.Conn, error)
7097

7198
type config struct {
72-
dialContext dial
73-
requestTimeout time.Duration
99+
dialContext dial
100+
requestTimeout time.Duration
101+
responseTimeout time.Duration
74102
}
75103

76104
type client struct {
@@ -90,7 +118,8 @@ type Client interface {
90118

91119
func New(options ...Option) (Client, error) {
92120
cfg := &config{
93-
requestTimeout: api.DefaultClientRequestTimeout,
121+
requestTimeout: api.DefaultClientRequestTimeout,
122+
responseTimeout: api.DefaultClientResponseHeaderTimeout,
94123
}
95124
for _, opt := range options {
96125
if err := opt(cfg); err != nil {
@@ -108,8 +137,8 @@ func New(options ...Option) (Client, error) {
108137
MaxIdleConnsPerHost: api.DefaultClientMaxIdleConnsPerHost,
109138
// keep the connection alive (good for long-lived clients)
110139
IdleConnTimeout: api.DefaultClientIdleConnTimeout,
111-
// Set short timeouts on headers
112-
ResponseHeaderTimeout: api.DefaultClientResponseHeaderTimeout,
140+
// By default it is 1 second, but can be overridden with [WithResponseTimeout]
141+
ResponseHeaderTimeout: cfg.responseTimeout,
113142
TLSHandshakeTimeout: api.DefaultClientTLSHandshakeTimeout,
114143

115144
DialContext: cfg.dialContext,

0 commit comments

Comments
 (0)