retry

package
v0.0.0-...-a05eeac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 5, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package retry defines a general library to handle errors and retries for various Azure clients.

Index

Constants

This section is empty.

Variables

View Source
var (

	// StatusCodesForRetry are a defined group of status code for which the client will retry.
	StatusCodesForRetry = []int{
		http.StatusRequestTimeout,
		http.StatusInternalServerError,
		http.StatusBadGateway,
		http.StatusServiceUnavailable,
		http.StatusGatewayTimeout,
	}
)

Functions

func DoExponentialBackoffRetry

func DoExponentialBackoffRetry(backoff *Backoff) autorest.SendDecorator

DoExponentialBackoffRetry represents an autorest.SendDecorator with backoff retry.

func HasStatusForbiddenOrIgnoredError

func HasStatusForbiddenOrIgnoredError(err error) bool

HasStatusForbiddenOrIgnoredError return true if the given error code is part of the error message This should only be used when trying to delete resources

func IsErrorRetriable

func IsErrorRetriable(err error) bool

IsErrorRetriable returns true if the error is retriable.

func NewRateLimiter

NewRateLimiter creates new read and write flowcontrol.RateLimiter from RateLimitConfig.

func RateLimitEnabled

func RateLimitEnabled(config *RateLimitConfig) bool

RateLimitEnabled returns true if CloudProviderRateLimit is set to true.

Types

type Backoff

type Backoff struct {
	// The initial duration.
	Duration time.Duration
	// Duration is multiplied by factor each iteration, if factor is not zero
	// and the limits imposed by Steps and Cap have not been reached.
	// Should not be negative.
	// The jitter does not contribute to the updates to the duration parameter.
	Factor float64
	// The sleep at each iteration is the duration plus an additional
	// amount chosen uniformly at random from the interval between
	// zero and `jitter*duration`.
	Jitter float64
	// The remaining number of iterations in which the duration
	// parameter may change (but progress can be stopped earlier by
	// hitting the cap). If not positive, the duration is not
	// changed. Used for exponential backoff in combination with
	// Factor and Cap.
	Steps int
	// A limit on revised values of the duration parameter. If a
	// multiplication by the factor parameter would make the duration
	// exceed the cap then the duration is set to the cap and the
	// steps parameter is set to zero.
	Cap time.Duration
	// The errors indicate that the request shouldn't do more retrying.
	NonRetriableErrors []string
	// The RetriableHTTPStatusCodes indicates that the HTTPStatusCode should do more retrying.
	RetriableHTTPStatusCodes []int
}

Backoff holds parameters applied to a Backoff function.

func NewBackoff

func NewBackoff(duration time.Duration, factor float64, jitter float64, steps int, cap time.Duration) *Backoff

NewBackoff creates a new Backoff.

func (*Backoff) Step

func (b *Backoff) Step() time.Duration

Step (1) returns an amount of time to sleep determined by the original Duration and Jitter and (2) mutates the provided Backoff to update its Steps and Duration.

func (*Backoff) WithNonRetriableErrors

func (b *Backoff) WithNonRetriableErrors(errs []string) *Backoff

WithNonRetriableErrors returns a new *Backoff with NonRetriableErrors assigned.

func (*Backoff) WithRetriableHTTPStatusCodes

func (b *Backoff) WithRetriableHTTPStatusCodes(httpStatusCodes []int) *Backoff

WithRetriableHTTPStatusCodes returns a new *Backoff with RetriableHTTPStatusCode assigned.

type ClientConfig

type ClientConfig struct {
	CloudName               string
	Location                string
	SubscriptionID          string
	ResourceManagerEndpoint string
	Authorizer              autorest.Authorizer
	RateLimitConfig         *RateLimitConfig
	Backoff                 *Backoff
	UserAgent               string
	DisableAzureStackCloud  bool
}

ClientConfig contains all essential information to create an Azure client.

func (*ClientConfig) WithRateLimiter

func (cfg *ClientConfig) WithRateLimiter(rl *RateLimitConfig) *ClientConfig

WithRateLimiter returns a new ClientConfig with rateLimitConfig set.

type Error

type Error struct {
	// Retriable indicates whether the request is retriable.
	Retriable bool
	// HTTPStatusCode indicates the response HTTP status code.
	HTTPStatusCode int
	// RetryAfter indicates the time when the request should retry after throttling.
	// A throttled request is retriable.
	RetryAfter time.Time
	// RetryAfter indicates the raw error from API.
	RawError error
}

Error indicates an error returned by Azure APIs.

func GetError

func GetError(resp *http.Response, err error) *Error

GetError gets a new Error based on resp and error.

func GetErrorWithRetriableHTTPStatusCodes

func GetErrorWithRetriableHTTPStatusCodes(resp *http.Response, err error, retriableHTTPStatusCodes []int) *Error

GetErrorWithRetriableHTTPStatusCodes gets an error with RetriableHTTPStatusCodes. It is used to retry on some HTTPStatusCodes.

func GetRateLimitError

func GetRateLimitError(isWrite bool, opName string) *Error

GetRateLimitError creates a new error for rate limiting.

func GetRetriableError

func GetRetriableError(err error) *Error

GetRetriableError gets new retriable Error.

func GetStatusNotFoundAndForbiddenIgnoredError

func GetStatusNotFoundAndForbiddenIgnoredError(resp *http.Response, err error) *Error

GetStatusNotFoundAndForbiddenIgnoredError gets an error with StatusNotFound and StatusForbidden ignored. It is only used in DELETE operations.

func GetThrottlingError

func GetThrottlingError(operation, reason string, retryAfter time.Time) *Error

GetThrottlingError creates a new error for throttling.

func NewError

func NewError(retriable bool, err error) *Error

NewError creates a new Error.

func (*Error) Error

func (err *Error) Error() error

Error returns the error. Note that Error doesn't implement error interface because (nil *Error) != (nil error).

func (*Error) IsNotFound

func (err *Error) IsNotFound() bool

IsNotFound returns true the if the requested object wasn't found

func (*Error) IsThrottled

func (err *Error) IsThrottled() bool

IsThrottled returns true the if the request is being throttled.

type RateLimitConfig

type RateLimitConfig struct {
	// Enable rate limiting
	CloudProviderRateLimit bool `json:"cloudProviderRateLimit,omitempty" yaml:"cloudProviderRateLimit,omitempty"`
	// Rate limit QPS (Read)
	CloudProviderRateLimitQPS float32 `json:"cloudProviderRateLimitQPS,omitempty" yaml:"cloudProviderRateLimitQPS,omitempty"`
	// Rate limit Bucket Size
	CloudProviderRateLimitBucket int `json:"cloudProviderRateLimitBucket,omitempty" yaml:"cloudProviderRateLimitBucket,omitempty"`
	// Rate limit QPS (Write)
	CloudProviderRateLimitQPSWrite float32 `json:"cloudProviderRateLimitQPSWrite,omitempty" yaml:"cloudProviderRateLimitQPSWrite,omitempty"`
	// Rate limit Bucket Size
	CloudProviderRateLimitBucketWrite int `json:"cloudProviderRateLimitBucketWrite,omitempty" yaml:"cloudProviderRateLimitBucketWrite,omitempty"`
}

RateLimitConfig indicates the rate limit config options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL