@@ -10,6 +10,7 @@ import (
10
10
"net/url"
11
11
"reflect"
12
12
"strings"
13
+ "time"
13
14
"unicode/utf8"
14
15
15
16
"github.com/tidepool-org/platform/errors"
@@ -35,6 +36,9 @@ type Client struct {
35
36
address string
36
37
userAgent string
37
38
errorResponseParser ErrorResponseParser
39
+
40
+ // DefaultRequestTimeout applies to requests whose context doesn't include a timeout.
41
+ DefaultRequestTimeout time.Duration
38
42
}
39
43
40
44
func New (cfg * Config ) (* Client , error ) {
@@ -49,12 +53,15 @@ func NewWithErrorParser(cfg *Config, errorResponseParser ErrorResponseParser) (*
49
53
}
50
54
51
55
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 ,
55
60
}, nil
56
61
}
57
62
63
+ const DefaultRequestTimeout = time .Minute
64
+
58
65
func (c * Client ) ConstructURL (paths ... string ) string {
59
66
segments := []string {}
60
67
for _ , path := range paths {
@@ -92,6 +99,12 @@ func (c *Client) RequestStreamWithHTTPClient(ctx context.Context, method string,
92
99
return nil , err
93
100
}
94
101
102
+ if _ , ok := ctx .Deadline (); ! ok {
103
+ toCtx , cancel := context .WithTimeout (ctx , c .DefaultRequestTimeout )
104
+ defer cancel ()
105
+ ctx = toCtx
106
+ }
107
+
95
108
res , err := httpClient .Do (req )
96
109
if err != nil {
97
110
return nil , errors .Wrapf (err , "unable to perform request to %s %s" , method , url )
0 commit comments