@@ -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
150152type 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+
391404func 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
891906retry:
892907 if tr != nil {
893- uaClient = newClient (tr )
908+ uaClient = newClient (tr , c . timeout )
894909 } else {
895910 uaClient = c .client
896911 }
0 commit comments