Skip to content

Commit b77c401

Browse files
authored
Merge pull request #458 from docker/client/response/timeout
feat: optional configuration to change response header timeout
2 parents d1f1e22 + 98fc178 commit b77c401

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

client/client.go

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

76+
// WithTimeout overrides the request timeout of the client.
77+
//
78+
// It is useful to set if there are hard-limits to when the client must wait
79+
// for the server to accept the request.
80+
//
81+
// A timout of 0 means no request timeout will be applied.
82+
// Negative durations are not allowed and will result in an error.
7683
func WithTimeout(timeout time.Duration) Option {
7784
return func(s *config) error {
85+
if timeout < 0 {
86+
return errors.New("request timeout duration cannot be negative")
87+
}
7888
s.requestTimeout = timeout
7989
return nil
8090
}
8191
}
8292

93+
// WithResponseTimeout overrides the response header timeout of the client.
94+
//
95+
// It is useful to set if there are long-lived user interactions required
96+
// when the Secrets Engine requests secrets from a plugin.
97+
//
98+
// A responseTimeout of 0 means no response header timeout will be applied.
99+
// Negative durations are not allowed and will result in an error.
100+
func WithResponseTimeout(responseTimeout time.Duration) Option {
101+
return func(s *config) error {
102+
if responseTimeout < 0 {
103+
return errors.New("response timeout duration cannot be negative")
104+
}
105+
s.responseTimeout = responseTimeout
106+
return nil
107+
}
108+
}
109+
83110
type dial func(ctx context.Context, network, addr string) (net.Conn, error)
84111

85112
type config struct {
86-
dialContext dial
87-
requestTimeout time.Duration
113+
dialContext dial
114+
requestTimeout time.Duration
115+
responseTimeout time.Duration
88116
}
89117

90118
type client struct {
@@ -104,7 +132,8 @@ type Client interface {
104132

105133
func New(options ...Option) (Client, error) {
106134
cfg := &config{
107-
requestTimeout: api.DefaultClientRequestTimeout,
135+
requestTimeout: api.DefaultClientRequestTimeout,
136+
responseTimeout: api.DefaultClientResponseHeaderTimeout,
108137
}
109138
for _, opt := range options {
110139
if err := opt(cfg); err != nil {
@@ -122,8 +151,8 @@ func New(options ...Option) (Client, error) {
122151
MaxIdleConnsPerHost: api.DefaultClientMaxIdleConnsPerHost,
123152
// keep the connection alive (good for long-lived clients)
124153
IdleConnTimeout: api.DefaultClientIdleConnTimeout,
125-
// Set short timeouts on headers
126-
ResponseHeaderTimeout: api.DefaultClientResponseHeaderTimeout,
154+
// By default it is 1 second, but can be overridden with [WithResponseTimeout]
155+
ResponseHeaderTimeout: cfg.responseTimeout,
127156
TLSHandshakeTimeout: api.DefaultClientTLSHandshakeTimeout,
128157

129158
DialContext: cfg.dialContext,

0 commit comments

Comments
 (0)