Compare commits
No commits in common. "0fe54519ea14bb0663d62eba4ac7bb9531733f36" and "726376f9a1c35778f2feb6d4d7a577a2cd86343c" have entirely different histories.
0fe54519ea
...
726376f9a1
1 changed files with 47 additions and 53 deletions
24
client.go
24
client.go
|
|
@ -29,23 +29,18 @@ func NewClient(key string, dryRun bool) *Client {
|
||||||
key: key,
|
key: key,
|
||||||
dryRun: dryRun,
|
dryRun: dryRun,
|
||||||
http: &http.Client{Timeout: 30 * time.Second},
|
http: &http.Client{Timeout: 30 * time.Second},
|
||||||
// limit to 2 requests per second, no bursts to avoid 429s
|
// 60 requests per 30 seconds: 2/sec sustained, burst of 60.
|
||||||
limiter: rate.NewLimiter(2, 1),
|
limiter: rate.NewLimiter(2, 60),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// do applies the rate limit, retries once on 429, and returns the raw body.
|
// do applies the rate limit, retries once on 429, and returns the raw body.
|
||||||
// rebuilds the body on every attempt
|
func (c *Client) do(ctx context.Context, method, path string, body io.Reader) ([]byte, error) {
|
||||||
func (c *Client) do(ctx context.Context, method, path string, body []byte) ([]byte, error) {
|
|
||||||
for attempt := 0; ; attempt++ {
|
|
||||||
if err := c.limiter.Wait(ctx); err != nil {
|
if err := c.limiter.Wait(ctx); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var r io.Reader
|
|
||||||
if body != nil {
|
req, err := http.NewRequestWithContext(ctx, method, baseURL+path, body)
|
||||||
r = bytes.NewReader(body) // fresh reader each attempt
|
|
||||||
}
|
|
||||||
req, err := http.NewRequestWithContext(ctx, method, baseURL+path, r)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -59,9 +54,9 @@ func (c *Client) do(ctx context.Context, method, path string, body []byte) ([]by
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusTooManyRequests && attempt < 3 {
|
if resp.StatusCode == http.StatusTooManyRequests {
|
||||||
resp.Body.Close()
|
|
||||||
wait := 30 * time.Second
|
wait := 30 * time.Second
|
||||||
if s := resp.Header.Get("Retry-After"); s != "" {
|
if s := resp.Header.Get("Retry-After"); s != "" {
|
||||||
if secs, err := strconv.Atoi(s); err == nil {
|
if secs, err := strconv.Atoi(s); err == nil {
|
||||||
|
|
@ -71,14 +66,13 @@ func (c *Client) do(ctx context.Context, method, path string, body []byte) ([]by
|
||||||
log.Printf("rate limited; waiting %s", wait)
|
log.Printf("rate limited; waiting %s", wait)
|
||||||
select {
|
select {
|
||||||
case <-time.After(wait):
|
case <-time.After(wait):
|
||||||
continue
|
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return nil, ctx.Err()
|
return nil, ctx.Err()
|
||||||
}
|
}
|
||||||
|
return c.do(ctx, method, path, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
raw, err := io.ReadAll(resp.Body)
|
raw, err := io.ReadAll(resp.Body)
|
||||||
resp.Body.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -87,7 +81,7 @@ func (c *Client) do(ctx context.Context, method, path string, body []byte) ([]by
|
||||||
}
|
}
|
||||||
return raw, nil
|
return raw, nil
|
||||||
}
|
}
|
||||||
}
|
|
||||||
func truncate(b []byte) string {
|
func truncate(b []byte) string {
|
||||||
if len(b) > 300 {
|
if len(b) > 300 {
|
||||||
return string(b[:300]) + "..."
|
return string(b[:300]) + "..."
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue