Skip to content

Commit aa189d4

Browse files
committed
use a default timeout on requests that don't specify one
This behavior was inadvertently lost in e282fb3, but is restored here, while still allowing for per-request timeouts to be specified.
1 parent e7151c3 commit aa189d4

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

client/client.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/url"
1111
"reflect"
1212
"strings"
13+
"time"
1314
"unicode/utf8"
1415

1516
"github.com/tidepool-org/platform/errors"
@@ -35,6 +36,9 @@ type Client struct {
3536
address string
3637
userAgent string
3738
errorResponseParser ErrorResponseParser
39+
40+
// DefaultRequestTimeout applies to requests whose context doesn't include a timeout.
41+
DefaultRequestTimeout time.Duration
3842
}
3943

4044
func New(cfg *Config) (*Client, error) {
@@ -49,12 +53,15 @@ func NewWithErrorParser(cfg *Config, errorResponseParser ErrorResponseParser) (*
4953
}
5054

5155
return &Client{
52-
address: cfg.Address,
53-
userAgent: cfg.UserAgent,
54-
errorResponseParser: errorResponseParser,
56+
address: cfg.Address,
57+
userAgent: cfg.UserAgent,
58+
errorResponseParser: errorResponseParser,
59+
DefaultRequestTimeout: DefaultRequestTimeout,
5560
}, nil
5661
}
5762

63+
const DefaultRequestTimeout = time.Minute
64+
5865
func (c *Client) ConstructURL(paths ...string) string {
5966
segments := []string{}
6067
for _, path := range paths {
@@ -92,6 +99,12 @@ func (c *Client) RequestStreamWithHTTPClient(ctx context.Context, method string,
9299
return nil, err
93100
}
94101

102+
if _, ok := ctx.Deadline(); !ok {
103+
toCtx, cancel := context.WithTimeout(ctx, c.DefaultRequestTimeout)
104+
defer cancel()
105+
ctx = toCtx
106+
}
107+
95108
res, err := httpClient.Do(req)
96109
if err != nil {
97110
return nil, errors.Wrapf(err, "unable to perform request to %s %s", method, url)

0 commit comments

Comments
 (0)