-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathclient.go
More file actions
312 lines (273 loc) · 7.94 KB
/
Copy pathclient.go
File metadata and controls
312 lines (273 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package quote
import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"strconv"
"time"
)
// Default API endpoints. Held as Client fields so tests can point at an
// httptest.Server; before this existed, the only injectable seam in the
// package was the tiingoFetch function variable.
const (
defaultTiingoBaseURL = "https://api.tiingo.com"
defaultCoinbaseBaseURL = "https://api.exchange.coinbase.com"
defaultNasdaqBaseURL = "https://api.nasdaq.com"
// Binance market data comes from data-api.binance.vision, not
// api.binance.com. The latter returns HTTP 451 ("restricted location")
// from outside the regions Binance serves, including the US - which is
// why the original binance support, removed in 2024, had stopped working.
// binance.vision serves the same /api/v3 endpoints for public market data
// with no key and no geo restriction.
//
// api.binance.us is a different exchange with its own symbol universe, so
// it is not a drop-in replacement for this base URL.
defaultBinanceBaseURL = "https://data-api.binance.vision"
)
// defaultHTTPClient is shared by every Client that does not supply its own.
// The previous code allocated an http.Client per call - and, in the Coinbase
// pager, per 200-bar page - which defeated connection pooling.
var defaultHTTPClient = &http.Client{Timeout: ClientTimeout}
// RetryPolicy controls retries for transport errors, 429, and 5xx responses.
// The zero value performs no retries, which is the historical behavior; opt in
// by setting Max. A Retry-After header on a 429 takes precedence over Backoff.
type RetryPolicy struct {
// Max is the number of additional attempts after the first.
Max int
// Backoff is the base delay, doubled after each attempt. Defaults to
// 500ms when Max > 0 and Backoff is zero.
Backoff time.Duration
}
func (p RetryPolicy) backoff(attempt int) time.Duration {
base := p.Backoff
if base <= 0 {
base = 500 * time.Millisecond
}
d := base << (attempt - 1)
if d > 30*time.Second {
d = 30 * time.Second
}
return d
}
// HTTPError reports a non-2xx response from a data source.
type HTTPError struct {
StatusCode int
Status string
URL string
Body []byte
// retryAfter carries a parsed Retry-After header, when present.
retryAfter time.Duration
hasRetryAfter bool
}
func (e *HTTPError) Error() string {
return fmt.Sprintf("http %s for %s", e.Status, e.URL)
}
// Client holds the configuration for fetching quotes: which HTTP client to
// use, how long to pause between requests, credentials, and where to log.
//
// The package-level functions delegate to DefaultClient, so existing code
// keeps working unchanged. Prefer a Client for new code: it accepts a
// context.Context, allows a per-caller timeout and retry policy, and does not
// rely on mutable package state.
type Client struct {
// HTTP is the underlying client. Defaults to a shared client with
// ClientTimeout.
HTTP *http.Client
// Delay is the pause between consecutive symbol requests. Unlike the
// deprecated package-level Delay, this is a real duration: use
// 100*time.Millisecond, not 100.
Delay time.Duration
// Log receives progress and error messages. Defaults to the package-level
// Log, which discards output unless SetOutput is called.
Log *log.Logger
// Token is the Tiingo API token used when a call does not supply one.
Token string
// UserAgent is sent with every request when non-empty.
UserAgent string
// Retry controls retry behavior. The zero value does not retry.
Retry RetryPolicy
// Base URLs, overridable in tests.
tiingoBase string
coinbaseBase string
nasdaqBase string
binanceBase string
}
// DefaultClient backs all package-level functions.
var DefaultClient = &Client{}
func (c *Client) httpClient() *http.Client {
if c != nil && c.HTTP != nil {
return c.HTTP
}
return defaultHTTPClient
}
func (c *Client) logger() *log.Logger {
if c != nil && c.Log != nil {
return c.Log
}
return Log
}
// rateLimit reports the pause between symbol requests.
//
// When Client.Delay is unset this falls back to the deprecated package-level
// Delay, which for historical reasons holds a raw millisecond count rather
// than a duration - quote.Delay = 100 means 100ms. Converting here keeps that
// meaning intact for existing callers while letting Client.Delay be an honest
// duration.
func (c *Client) rateLimit() time.Duration {
if c != nil && c.Delay != 0 {
return c.Delay
}
return Delay * time.Millisecond
}
func (c *Client) token(override string) string {
if override != "" {
return override
}
if c != nil {
return c.Token
}
return ""
}
func (c *Client) tiingoURL() string {
if c != nil && c.tiingoBase != "" {
return c.tiingoBase
}
return defaultTiingoBaseURL
}
func (c *Client) coinbaseURL() string {
if c != nil && c.coinbaseBase != "" {
return c.coinbaseBase
}
return defaultCoinbaseBaseURL
}
func (c *Client) nasdaqURL() string {
if c != nil && c.nasdaqBase != "" {
return c.nasdaqBase
}
return defaultNasdaqBaseURL
}
func (c *Client) binanceURL() string {
if c != nil && c.binanceBase != "" {
return c.binanceBase
}
return defaultBinanceBaseURL
}
// sleep pauses for d, returning early if ctx is cancelled.
func sleep(ctx context.Context, d time.Duration) error {
if d <= 0 {
return ctx.Err()
}
t := time.NewTimer(d)
defer t.Stop()
select {
case <-ctx.Done():
return ctx.Err()
case <-t.C:
return nil
}
}
// retryAfter reads a Retry-After header expressed in seconds.
func retryAfter(resp *http.Response) (time.Duration, bool) {
v := resp.Header.Get("Retry-After")
if v == "" {
return 0, false
}
secs, err := strconv.Atoi(v)
if err != nil || secs < 0 {
return 0, false
}
return time.Duration(secs) * time.Second, true
}
func retryableStatus(code int) bool {
return code == http.StatusTooManyRequests || code >= 500
}
// get performs a GET and returns the response body.
//
// It centralizes what was previously duplicated or missing across the five
// call sites: the http.NewRequest error was discarded everywhere (`req, _ :=`),
// so a malformed URL panicked on the following header write; status codes were
// checked in only one of the five; and response bodies were closed with a
// deferred call inside a paging loop.
func (c *Client) get(ctx context.Context, url string, header http.Header) ([]byte, error) {
if ctx == nil {
ctx = context.Background()
}
client := c.httpClient()
attempts := 1 + max(c.Retry.Max, 0)
var lastErr error
for attempt := range attempts {
if attempt > 0 {
wait := c.Retry.backoff(attempt)
var he *HTTPError
if errors.As(lastErr, &he) && he.hasRetryAfter {
wait = he.retryAfter
}
if err := sleep(ctx, wait); err != nil {
return nil, err
}
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
// A bad URL will not fix itself; do not retry.
return nil, err
}
for k, vs := range header {
for _, v := range vs {
req.Header.Add(k, v)
}
}
if c != nil && c.UserAgent != "" {
req.Header.Set("User-Agent", c.UserAgent)
}
resp, err := client.Do(req)
if err != nil {
if ctx.Err() != nil {
return nil, ctx.Err()
}
lastErr = err
continue
}
body, readErr := io.ReadAll(resp.Body)
closeErr := resp.Body.Close()
if readErr != nil {
if ctx.Err() != nil {
return nil, ctx.Err()
}
lastErr = readErr
continue
}
if closeErr != nil {
lastErr = closeErr
continue
}
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return body, nil
}
httpErr := &HTTPError{
StatusCode: resp.StatusCode,
Status: resp.Status,
URL: url,
Body: body,
}
if d, ok := retryAfter(resp); ok {
httpErr.retryAfter = d
httpErr.hasRetryAfter = true
}
lastErr = httpErr
if !retryableStatus(resp.StatusCode) {
return nil, httpErr
}
}
return nil, lastErr
}
// statusCode reports the HTTP status carried by err, if any.
func statusCode(err error) (int, bool) {
var he *HTTPError
if errors.As(err, &he) {
return he.StatusCode, true
}
return 0, false
}