context

package
v2.13.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: Apache-2.0 Imports: 6 Imported by: 31

Documentation

Overview

Package context holds the last resort overrides and fyi objects that can be passed to clients and transports added to context.Context objects.

Index

Constants

View Source
const (
	BackoffStrategyNone        = "none"
	BackoffStrategyConstant    = "constant"
	BackoffStrategyLinear      = "linear"
	BackoffStrategyExponential = "exponential"
)

Variables

View Source
var DefaultRetryParams = RetryParams{Strategy: BackoffStrategyNone}

Functions

func LoggerFrom

func LoggerFrom(ctx context.Context) *zap.SugaredLogger

LoggerFrom returns the logger stored in context.

func TargetFrom

func TargetFrom(ctx context.Context) *url.URL

TargetFrom looks in the given context and returns `target` as a parsed url if found and valid, otherwise nil.

func TopicFrom

func TopicFrom(ctx context.Context) string

TopicFrom looks in the given context and returns `topic` as a string if found and valid, otherwise "".

func ValuesDelegating added in v2.6.1

func ValuesDelegating(child, parent context.Context) context.Context

ValuesDelegating wraps a child and parent context. It will perform Value() lookups first on the child, and then fall back to the child. All other calls go solely to the child context.

func WithLogger

func WithLogger(ctx context.Context, logger *zap.SugaredLogger) context.Context

WithLogger returns a new context with the logger injected into the given context.

func WithRetriesConstantBackoff

func WithRetriesConstantBackoff(ctx context.Context, delay time.Duration, maxTries int) context.Context

WithRetriesConstantBackoff returns back a new context with retries parameters using constant backoff strategy. MaxTries is the maximum number for retries and delay is the time interval between retries

func WithRetriesExponentialBackoff

func WithRetriesExponentialBackoff(ctx context.Context, period time.Duration, maxTries int) context.Context

WithRetriesExponentialBackoff returns back a new context with retries parameters using exponential backoff strategy. MaxTries is the maximum number for retries and period is the amount of time to wait, used as `period * 2^retries`.

func WithRetriesLinearBackoff

func WithRetriesLinearBackoff(ctx context.Context, delay time.Duration, maxTries int) context.Context

WithRetriesLinearBackoff returns back a new context with retries parameters using linear backoff strategy. MaxTries is the maximum number for retries and delay*tries is the time interval between retries

func WithRetryParams

func WithRetryParams(ctx context.Context, rp *RetryParams) context.Context

WithRetryParams returns back a new context with retries parameters.

func WithTarget

func WithTarget(ctx context.Context, target string) context.Context

WithTarget returns back a new context with the given target. Target is intended to be transport dependent. For http transport, `target` should be a full URL and will be injected into the outbound http request.

func WithTopic

func WithTopic(ctx context.Context, topic string) context.Context

WithTopic returns back a new context with the given topic. Topic is intended to be transport dependent. For pubsub transport, `topic` should be a Pub/Sub Topic ID.

Types

type BackoffStrategy

type BackoffStrategy string

type RetryParams

type RetryParams struct {
	// Strategy is the backoff strategy to applies between retries
	Strategy BackoffStrategy

	// MaxTries is the maximum number of times to retry request before giving up
	MaxTries int

	// Period is
	// - for none strategy: no delay
	// - for constant strategy: the delay interval between retries
	// - for linear strategy: interval between retries = Period * retries
	// - for exponential strategy: interval between retries = Period * retries^2
	Period time.Duration
}

RetryParams holds parameters applied to retries

func RetriesFrom

func RetriesFrom(ctx context.Context) *RetryParams

RetriesFrom looks in the given context and returns the retries parameters if found. Otherwise returns the default retries configuration (ie. no retries).

func (*RetryParams) Backoff

func (r *RetryParams) Backoff(ctx context.Context, tries int) error

Backoff is a blocking call to wait for the correct amount of time for the retry. `tries` is assumed to be the number of times the caller has already retried.

func (*RetryParams) BackoffFor

func (r *RetryParams) BackoffFor(tries int) time.Duration

BackoffFor tries will return the time duration that should be used for this current try count. `tries` is assumed to be the number of times the caller has already retried.

Jump to

Keyboard shortcuts

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