Skip to content

Commit d8993ac

Browse files
marainohslatman
authored andcommitted
Add option to specify a client timeout
This commit adds to the ca Client a new option to specify the client timeout. Fixes #2176
1 parent dd8376c commit d8993ac

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

ca/adminClient.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func NewAdminClient(endpoint string, opts ...ClientOption) (*AdminClient, error)
7777
}
7878

7979
return &AdminClient{
80-
client: newClient(tr),
80+
client: newClient(tr, o.timeout),
8181
endpoint: u,
8282
retryFunc: o.retryFunc,
8383
opts: opts,

ca/client.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path/filepath"
2323
"strconv"
2424
"strings"
25+
"time"
2526

2627
"github.com/pkg/errors"
2728
"golang.org/x/net/http2"
@@ -53,10 +54,11 @@ type uaClient struct {
5354
Client *http.Client
5455
}
5556

56-
func newClient(transport http.RoundTripper) *uaClient {
57+
func newClient(transport http.RoundTripper, timeout time.Duration) *uaClient {
5758
return &uaClient{
5859
Client: &http.Client{
5960
Transport: transport,
61+
Timeout: timeout,
6062
},
6163
}
6264
}
@@ -149,6 +151,7 @@ type ClientOption func(o *clientOptions) error
149151

150152
type clientOptions struct {
151153
transport http.RoundTripper
154+
timeout time.Duration
152155
rootSHA256 string
153156
rootFilename string
154157
rootBundle []byte
@@ -388,6 +391,16 @@ func WithRetryFunc(fn RetryFunc) ClientOption {
388391
}
389392
}
390393

394+
// WithTimeout defines the time limit for requests made by this client. The
395+
// timeout includes connection time, any redirects, and reading the response
396+
// body.
397+
func WithTimeout(d time.Duration) ClientOption {
398+
return func(o *clientOptions) error {
399+
o.timeout = d
400+
return nil
401+
}
402+
}
403+
391404
func getTransportFromFile(filename string) (http.RoundTripper, error) {
392405
data, err := os.ReadFile(filename)
393406
if err != nil {
@@ -548,6 +561,7 @@ type Client struct {
548561
client *uaClient
549562
endpoint *url.URL
550563
retryFunc RetryFunc
564+
timeout time.Duration
551565
opts []ClientOption
552566
}
553567

@@ -568,9 +582,10 @@ func NewClient(endpoint string, opts ...ClientOption) (*Client, error) {
568582
}
569583

570584
return &Client{
571-
client: newClient(tr),
585+
client: newClient(tr, o.timeout),
572586
endpoint: u,
573587
retryFunc: o.retryFunc,
588+
timeout: o.timeout,
574589
opts: opts,
575590
}, nil
576591
}
@@ -890,7 +905,7 @@ func (c *Client) RevokeWithContext(ctx context.Context, req *api.RevokeRequest,
890905
var uaClient *uaClient
891906
retry:
892907
if tr != nil {
893-
uaClient = newClient(tr)
908+
uaClient = newClient(tr, c.timeout)
894909
} else {
895910
uaClient = c.client
896911
}

ca/client_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,34 @@ func TestClient_GetCaURL(t *testing.T) {
10171017
}
10181018
}
10191019

1020+
func TestClient_WithTimeout(t *testing.T) {
1021+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1022+
time.Sleep(100 * time.Millisecond)
1023+
render.JSONStatus(w, r, api.HealthResponse{Status: "ok"}, 200)
1024+
}))
1025+
defer srv.Close()
1026+
1027+
tests := []struct {
1028+
name string
1029+
options []ClientOption
1030+
assertion assert.ErrorAssertionFunc
1031+
}{
1032+
{"ok", []ClientOption{WithTransport(http.DefaultTransport)}, assert.NoError},
1033+
{"ok with timeout", []ClientOption{WithTransport(http.DefaultTransport), WithTimeout(time.Second)}, assert.NoError},
1034+
{"fail with timeout", []ClientOption{WithTransport(http.DefaultTransport), WithTimeout(100 * time.Millisecond)}, assert.Error},
1035+
}
1036+
1037+
for _, tt := range tests {
1038+
t.Run(tt.name, func(t *testing.T) {
1039+
c, err := NewClient(srv.URL, tt.options...)
1040+
require.NoError(t, err)
1041+
_, err = c.Health()
1042+
tt.assertion(t, err)
1043+
})
1044+
}
1045+
1046+
}
1047+
10201048
func Test_enforceRequestID(t *testing.T) {
10211049
set := httptest.NewRequest(http.MethodGet, "https://example.com", http.NoBody)
10221050
set.Header.Set("X-Request-Id", "already-set")

0 commit comments

Comments
 (0)