Use a slightly more aggressive backoff approach

Doubling the backoff every iteration turned out to be a bit
too relaxed for the common situations where you run into API
throttling. This change gives the API enough room to breathe,
but re-tries requests just a little more often.
pull/109/head
Christian Muehlhaeuser 2019-08-08 09:26:16 +02:00 committed by mattn
parent 26fcedc8aa
commit 8a48862adc
1 changed files with 3 additions and 2 deletions

View File

@ -123,7 +123,7 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
} }
var resp *http.Response var resp *http.Response
backoff := 1000 * time.Millisecond backoff := time.Second
for { for {
resp, err = c.Do(req) resp, err = c.Do(req)
if err != nil { if err != nil {
@ -137,13 +137,14 @@ func (c *Client) doAPI(ctx context.Context, method string, uri string, params in
if backoff > time.Hour { if backoff > time.Hour {
break break
} }
backoff *= 2
select { select {
case <-time.After(backoff): case <-time.After(backoff):
case <-ctx.Done(): case <-ctx.Done():
return ctx.Err() return ctx.Err()
} }
backoff = time.Duration(1.5 * float64(backoff))
continue continue
} }
break break