Documentation
¶
Overview ¶
Package linger contains a set of utilities for dealing with common and repetitive timeout and retry logic.
Index ¶
- Constants
- func Coalesce(values ...time.Duration) (v time.Duration, ok bool)
- func CoalesceT(values ...time.Time) (v time.Time, ok bool)
- func ContextWithTimeout(ctx context.Context, durations ...time.Duration) (context.Context, func())
- func ContextWithTimeoutX(ctx context.Context, x DurationTransform, durations ...time.Duration) (context.Context, func())
- func Divide(d time.Duration, v float64) time.Duration
- func Earliest(times ...time.Time) time.Time
- func First(p DurationPredicate, values ...time.Duration) (v time.Duration, ok bool)
- func FirstT(p TimePredicate, values ...time.Time) (v time.Time, ok bool)
- func FromContextDeadline(ctx context.Context) (d time.Duration, ok bool)
- func FromSeconds(s float64) time.Duration
- func FullJitter(d time.Duration) time.Duration
- func Identity(d time.Duration) time.Duration
- func Latest(times ...time.Time) time.Time
- func Limit(d, a, b time.Duration) time.Duration
- func LimitT(t, a, b time.Time) time.Time
- func Longest(durations ...time.Duration) time.Duration
- func Multiply(d time.Duration, v float64) time.Duration
- func MustCoalesce(values ...time.Duration) time.Duration
- func MustCoalesceT(values ...time.Time) time.Time
- func MustFirst(p DurationPredicate, values ...time.Duration) time.Duration
- func MustFirstT(p TimePredicate, values ...time.Time) time.Time
- func Negative(d time.Duration) bool
- func NonZero(d time.Duration) bool
- func NonZeroT(t time.Time) bool
- func Positive(d time.Duration) bool
- func Rand(a, b time.Duration) time.Duration
- func Shortest(durations ...time.Duration) time.Duration
- func Sleep(ctx context.Context, durations ...time.Duration) error
- func SleepUntil(ctx context.Context, times ...time.Time) error
- func SleepUntilX(ctx context.Context, x DurationTransform, times ...time.Time) error
- func SleepX(ctx context.Context, x DurationTransform, durations ...time.Duration) error
- type DurationPredicate
- type DurationTransform
- func Coalescer(values ...time.Duration) DurationTransform
- func Defaulter(p DurationPredicate, values ...time.Duration) DurationTransform
- func Divider(v float64) DurationTransform
- func Limiter(a, b time.Duration) DurationTransform
- func Multiplier(v float64) DurationTransform
- func ProportionalJitter(p float64) DurationTransform
- type TimePredicate
Constants ¶
Variables ¶
This section is empty.
Functions ¶
func Coalesce ¶
Coalesce returns the first of its arguments that is positive.
If none of the arguments are positive, v is the zero-value and ok is false.
func CoalesceT ¶
CoalesceT returns the first of its arguments that is non-zero.
If none of the arguments are non-zero, v is the zero-value and ok is false.
func ContextWithTimeout ¶
func ContextWithTimeout( ctx context.Context, durations ...time.Duration, ) (context.Context, func())
ContextWithTimeout returns a context with a deadline some duration after the current time.
The timeout duration is computed by finding the first of the supplied durations that is positive. It uses a zero duration if none of the supplied durations are positive.
func ContextWithTimeoutX ¶
func ContextWithTimeoutX( ctx context.Context, x DurationTransform, durations ...time.Duration, ) (context.Context, func())
ContextWithTimeoutX returns a context with a deadline some duration after the current time.
The timeout duration is computed by finding the first of the supplied durations that is positive, then applying the transform x. It uses a zero duration if none of the supplied durations are positive.
The transform can be used to apply jitter to the timeout duration, for example, by using one of the built-in jitter transforms such as FullJitter() or ProportionalJitter().
func Earliest ¶
Earliest returns the earliest of the given times.
It returns the zero-value if no times are supplied.
func First ¶
First returns the first of its arguments for which the predicate function p returns true.
If the p returns false for all values, v is the zero-value and ok is false.
func FirstT ¶
FirstT returns the first of its arguments for which the predicate function p returns true.
If the p returns false for all values, v is the zero-value and ok is false.
func FromContextDeadline ¶
FromContextDeadline returns the duration until the deadline of ctx is reached.
ok is false if ctx does not have a deadline.
func FromSeconds ¶
FromSeconds returns a duration equivalent to the given number of seconds.
func FullJitter ¶
FullJitter is a DurationTransform that applies "full jitter" to the input duration.
The output duration is a random duration between 0 and the input duration, inclusive.
func Latest ¶
Latest returns the latest of the given times.
It returns the zero-value if no times are supplied.
func Longest ¶
Longest returns the largest of the given durations.
It returns MinDuration if no durations are supplied.
func MustCoalesce ¶
MustCoalesce returns the first of its arguments that is positive.
It panics if none of the arguments are positive.
func MustCoalesceT ¶
MustCoalesceT returns the first of its arguments that is non-zero.
It panics if none of the arguments are non-zero.
func MustFirst ¶
func MustFirst(p DurationPredicate, values ...time.Duration) time.Duration
MustFirst returns the first of its arguments for which the predicate function p returns true.
It panics if p returns false for all values.
func MustFirstT ¶
func MustFirstT(p TimePredicate, values ...time.Time) time.Time
MustFirstT returns the first of its arguments for which the predicate function p returns true.
It panics if p returns false for all values.
func Shortest ¶
Shortest returns the smallest of the given durations.
It returns MaxDuration if no durations are supplied.
func Sleep ¶
Sleep pauses the current goroutine until some duration has elapsed.
The sleep duration computed by finding the first of the supplied durations that is positive. It returns immediately if none of the supplied durations are positive.
It sleeps until the duration elapses or ctx is canceled, whichever is first. If ctx is canceled before the duration elapses it returns ctx.Err(), otherwise it returns nil.
func SleepUntil ¶
SleepUntil pauses the current goroutine until a specific time.
The sleep duration is computed by finding the amount of time until the earliest of the supplied times. It returns immediately if the earliest of the times is in the past.
It sleeps until the time is reached or ctx is canceled, whichever is first. If ctx is canceled before the time is reached it returns ctx.Err(), otherwise it returns nil.
func SleepUntilX ¶
SleepUntilX pauses the current goroutine until a specific time.
The sleep duration is computed by finding the amount of time until the earliest of the supplied times, then applying the transform x. It returns immediately if the earliest of the times is in the past.
The transform can be used to apply jitter to the sleep duration, for example, by using one of the built-in jitter transforms such as FullJitter() or ProportionalJitter().
It sleeps until the time is reached or ctx is canceled, whichever is first. If ctx is canceled before the time is reached it returns ctx.Err(), otherwise it returns nil.
func SleepX ¶
SleepX pauses the current goroutine until some duration has elapsed.
The sleep duration computed by finding the first of the supplied durations that is positive, then applying the transform x. It returns immediately if none of the supplied durations are positive.
The transform can be used to apply jitter to the sleep duration, for example, by using one of the built-in jitter transforms such as FullJitter() or ProportionalJitter().
It sleeps until the duration elapses or ctx is canceled, whichever is first. If ctx is canceled before the duration elapses it returns ctx.Err(), otherwise it returns nil.
Types ¶
type DurationPredicate ¶
DurationPredicate is a predicate function for time.Duration values.
type DurationTransform ¶
DurationTransform is a function that applies some transformation to time.Duration values.
func Coalescer ¶ added in v0.2.0
func Coalescer(values ...time.Duration) DurationTransform
Coalescer returns a DurationTransform that falls back to the first positive value.
The transform input value is checked first, then each of the given values in order. It panics if none of the values are positive.
func Defaulter ¶ added in v0.2.0
func Defaulter(p DurationPredicate, values ...time.Duration) DurationTransform
Defaulter returns a DurationTransform that falls back to the first value for which the predicate function p returns true.
The transform input value is checked first, then each of the given values in order. It panics if p returns false for all values.
func Divider ¶ added in v0.2.0
func Divider(v float64) DurationTransform
Divider returns a DurationTransform that divides the input duration by v.
func Limiter ¶ added in v0.2.0
func Limiter(a, b time.Duration) DurationTransform
Limiter returns a DurationTransform that limits the input duration between a and b, inclusive.
func Multiplier ¶ added in v0.2.0
func Multiplier(v float64) DurationTransform
Multiplier returns a DurationTransform that multiplies the input duration by v.
func ProportionalJitter ¶
func ProportionalJitter(p float64) DurationTransform
ProportionalJitter returns a DurationTransform that applies "proportional jitter" to the input duration.
The returned transform generates a random jitter amount proportional to the input duration. This jitter amount is added to the input duration to produce the output duration.
The jitter proportion is given by p. For example, a value of 0.1 adds up to 10% to the input duration. p may be negative to indicate that the jitter amount should be subtracted from the input duration.
type TimePredicate ¶
TimePredicate is a predicate function for time.Time values.