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
- Variables
- func LoggerFrom(ctx context.Context) *zap.SugaredLogger
- func TargetFrom(ctx context.Context) *url.URL
- func TopicFrom(ctx context.Context) string
- func ValuesDelegating(child, parent context.Context) context.Context
- func WithLogger(ctx context.Context, logger *zap.SugaredLogger) context.Context
- func WithRetriesConstantBackoff(ctx context.Context, delay time.Duration, maxTries int) context.Context
- func WithRetriesExponentialBackoff(ctx context.Context, period time.Duration, maxTries int) context.Context
- func WithRetriesLinearBackoff(ctx context.Context, delay time.Duration, maxTries int) context.Context
- func WithRetryParams(ctx context.Context, rp *RetryParams) context.Context
- func WithTarget(ctx context.Context, target string) context.Context
- func WithTopic(ctx context.Context, topic string) context.Context
- type BackoffStrategy
- type RetryParams
Constants ¶
const ( BackoffStrategyNone = "none" BackoffStrategyConstant = "constant" BackoffStrategyLinear = "linear" BackoffStrategyExponential = "exponential" )
Variables ¶
var DefaultRetryParams = RetryParams{Strategy: BackoffStrategyNone}
Functions ¶
func LoggerFrom ¶
func LoggerFrom(ctx context.Context) *zap.SugaredLogger
LoggerFrom returns the logger stored in context.
func TargetFrom ¶
TargetFrom looks in the given context and returns `target` as a parsed url if found and valid, otherwise nil.
func TopicFrom ¶
TopicFrom looks in the given context and returns `topic` as a string if found and valid, otherwise "".
func ValuesDelegating ¶
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 ¶
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 ¶
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.
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.