rebuild the body on every attempt
This commit is contained in:
parent
726376f9a1
commit
18e8cb055e
1 changed files with 46 additions and 40 deletions
20
client.go
20
client.go
|
|
@ -35,12 +35,17 @@ func NewClient(key string, dryRun bool) *Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
func (c *Client) do(ctx context.Context, method, path string, body io.Reader) ([]byte, error) {
|
// rebuilds the body on every attempt
|
||||||
|
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
|
||||||
req, err := http.NewRequestWithContext(ctx, method, baseURL+path, body)
|
if body != nil {
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
@ -54,9 +59,9 @@ func (c *Client) do(ctx context.Context, method, path string, body io.Reader) ([
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode == http.StatusTooManyRequests {
|
if resp.StatusCode == http.StatusTooManyRequests && attempt < 3 {
|
||||||
|
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 {
|
||||||
|
|
@ -66,13 +71,14 @@ func (c *Client) do(ctx context.Context, method, path string, body io.Reader) ([
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
@ -80,8 +86,8 @@ func (c *Client) do(ctx context.Context, method, path string, body io.Reader) ([
|
||||||
return nil, fmt.Errorf("%s %s: %d: %s", method, path, resp.StatusCode, truncate(raw))
|
return nil, fmt.Errorf("%s %s: %d: %s", method, path, resp.StatusCode, truncate(raw))
|
||||||
}
|
}
|
||||||
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