Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions logreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ func (r *LogReader) read(l []byte) (int, error) {
}

// backoff will perform exponential backoff based on the iteration and
// limited by the provided min and max (in milliseconds) durations.
func backoff(min, max float64, iter int) time.Duration {
backoff := math.Pow(2, float64(iter)/5) * min
if backoff > max {
backoff = max
// limited by the provided minimum and maximum (in milliseconds) durations.
func backoff(minimum, maximum float64, iter int) time.Duration {
backoff := math.Pow(2, float64(iter)/5) * minimum
if backoff > maximum {
backoff = maximum
}
return time.Duration(backoff) * time.Millisecond
}
24 changes: 12 additions & 12 deletions tfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,36 +617,36 @@ func (c *Client) retryHTTPCheck(ctx context.Context, resp *http.Response, err er

// retryHTTPBackoff provides a generic callback for Client.Backoff which
// will pass through all calls based on the status code of the response.
func (c *Client) retryHTTPBackoff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
func (c *Client) retryHTTPBackoff(minimum, maximum time.Duration, attemptNum int, resp *http.Response) time.Duration {
if c.retryLogHook != nil {
c.retryLogHook(attemptNum, resp)
}

// Use the rate limit backoff function when we are rate limited.
if resp != nil && resp.StatusCode == 429 {
return rateLimitBackoff(min, max, resp)
return rateLimitBackoff(minimum, maximum, resp)
}

// Set custom duration's when we experience a service interruption.
min = 700 * time.Millisecond
max = 900 * time.Millisecond
minimum = 700 * time.Millisecond
maximum = 900 * time.Millisecond

return retryablehttp.LinearJitterBackoff(min, max, attemptNum, resp)
return retryablehttp.LinearJitterBackoff(minimum, maximum, attemptNum, resp)
}

// rateLimitBackoff provides a callback for Client.Backoff which will use the
// X-RateLimit_Reset header to determine the time to wait. We add some jitter
// to prevent a thundering herd.
//
// min and max are mainly used for bounding the jitter that will be added to
// minimum and maximum are mainly used for bounding the jitter that will be added to
// the reset time retrieved from the headers. But if the final wait time is
// less than min, min will be used instead.
func rateLimitBackoff(min, max time.Duration, resp *http.Response) time.Duration {
// less than minimum, minimum will be used instead.
func rateLimitBackoff(minimum, maximum time.Duration, resp *http.Response) time.Duration {
// rnd is used to generate pseudo-random numbers.
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))

// First create some jitter bounded by the min and max durations.
jitter := time.Duration(rnd.Float64() * float64(max-min))
jitter := time.Duration(rnd.Float64() * float64(maximum-minimum))

if resp != nil && resp.Header.Get(_headerRateReset) != "" {
v := resp.Header.Get(_headerRateReset)
Expand All @@ -655,12 +655,12 @@ func rateLimitBackoff(min, max time.Duration, resp *http.Response) time.Duration
log.Fatal(err)
}
// Only update min if the given time to wait is longer
if reset > 0 && time.Duration(reset*1e9) > min {
min = time.Duration(reset * 1e9)
if reset > 0 && time.Duration(reset*1e9) > minimum {
minimum = time.Duration(reset * 1e9)
}
}

return min + jitter
return minimum + jitter
}

type rawAPIMetadata struct {
Expand Down
Loading