github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/storage/chunk/client/aws/retryer.go (about)

     1  package aws
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"github.com/aws/aws-sdk-go/aws/request"
     8  	"github.com/grafana/dskit/backoff"
     9  	ot "github.com/opentracing/opentracing-go"
    10  	otlog "github.com/opentracing/opentracing-go/log"
    11  )
    12  
    13  // Map Cortex Backoff into AWS Retryer interface
    14  type retryer struct {
    15  	*backoff.Backoff
    16  	maxRetries int
    17  }
    18  
    19  var _ request.Retryer = &retryer{}
    20  
    21  func newRetryer(ctx context.Context, cfg backoff.Config) *retryer {
    22  	return &retryer{
    23  		Backoff:    backoff.New(ctx, cfg),
    24  		maxRetries: cfg.MaxRetries,
    25  	}
    26  }
    27  
    28  func (r *retryer) withRetries(req *request.Request) {
    29  	req.Retryer = r
    30  }
    31  
    32  // RetryRules return the retry delay that should be used by the SDK before
    33  // making another request attempt for the failed request.
    34  func (r *retryer) RetryRules(req *request.Request) time.Duration {
    35  	duration := r.Backoff.NextDelay()
    36  	if sp := ot.SpanFromContext(req.Context()); sp != nil {
    37  		sp.LogFields(otlog.Int("retry", r.NumRetries()))
    38  	}
    39  	return duration
    40  }
    41  
    42  // ShouldRetry returns if the failed request is retryable.
    43  func (r *retryer) ShouldRetry(req *request.Request) bool {
    44  	return r.Ongoing() && (req.IsErrorRetryable() || req.IsErrorThrottle())
    45  }
    46  
    47  // MaxRetries is the number of times a request may be retried before
    48  // failing.
    49  func (r *retryer) MaxRetries() int {
    50  	return r.maxRetries
    51  }