Skip to content

Commit

Permalink
Merge pull request #157 from coinbase/patrick/print-retry-number
Browse files Browse the repository at this point in the history
[Fetcher] Log retry attempts
  • Loading branch information
patrick-ogrady authored Sep 15, 2020
2 parents d4e1946 + 76c0676 commit 34d7fa0
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions fetcher/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,23 @@ const (
clientTimeout = "Client.Timeout exceeded"
)

// Backoff wraps backoff.BackOff so we can
// access the retry count (which is private
// on backoff.BackOff).
type Backoff struct {
backoff backoff.BackOff
attempts int
}

// backoffRetries creates the backoff.BackOff struct used by all
// *Retry functions in the fetcher.
func backoffRetries(
maxElapsedTime time.Duration,
maxRetries uint64,
) backoff.BackOff {
) *Backoff {
exponentialBackoff := backoff.NewExponentialBackOff()
exponentialBackoff.MaxElapsedTime = maxElapsedTime
return backoff.WithMaxRetries(exponentialBackoff, maxRetries)
return &Backoff{backoff: backoff.WithMaxRetries(exponentialBackoff, maxRetries)}
}

// transientError returns a boolean indicating if a particular
Expand All @@ -65,15 +73,15 @@ func transientError(err error) bool {

// tryAgain handles a backoff and prints error messages depending
// on the fetchMsg.
func tryAgain(fetchMsg string, thisBackoff backoff.BackOff, err *Error) *Error {
func tryAgain(fetchMsg string, thisBackoff *Backoff, err *Error) *Error {
// Only retry if an error is explicitly retriable or the server
// returned a transient error.
if !transientError(err.Err) &&
(err.ClientErr == nil || !err.ClientErr.Retriable) {
return err
}

nextBackoff := thisBackoff.NextBackOff()
nextBackoff := thisBackoff.backoff.NextBackOff()
if nextBackoff == backoff.Stop {
return &Error{
Err: fmt.Errorf(
Expand All @@ -89,11 +97,13 @@ func tryAgain(fetchMsg string, thisBackoff backoff.BackOff, err *Error) *Error {
errMessage = types.PrintStruct(err.ClientErr)
}

thisBackoff.attempts++
log.Printf(
"%s: retrying fetch for %s after %fs\n",
"%s: retrying fetch for %s after %fs (prior attempts: %d)\n",
errMessage,
fetchMsg,
nextBackoff.Seconds(),
thisBackoff.attempts,
)
time.Sleep(nextBackoff)

Expand Down

0 comments on commit 34d7fa0

Please sign in to comment.