time

package
v0.0.113 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2021 License: MIT Imports: 6 Imported by: 6

Documentation

Index

Constants

View Source
const (

	// The default initial interval value (0.5 seconds).
	DefaultInitialInterval = 500 * time.Millisecond

	// The default randomization factor (0.5 which results in a random period ranging between 50%
	// below and 50% above the retry interval).
	DefaultRandomizationFactor = 0.5

	// The default multiplier value (1.5 which is 50% increase per back off).
	DefaultMultiplier = 1.5

	// The default maximum back off time (1 minute).
	DefaultMaxInterval = time.Minute

	// The default maximum elapsed time (15 minutes).
	DefaultMaxElapsedDuration = 15 * time.Minute
)
View Source
const DefaultBaseDuration = 5 * time.Millisecond
View Source
const DefaultMaxDuration = 1 * time.Second
View Source
const InfDuration = time.Duration(1<<63 - 1)

InfDuration is the duration returned by Delay when a Reservation is not OK.

View Source
const ZeroDuration = 0

Variables

This section is empty.

Functions

func After

func After(d time.Duration) <-chan time.Time

func BackoffUntil added in v0.0.95

func BackoffUntil(ctx context.Context, f func(ctx context.Context), backoff BackOff, sliding bool)

BackoffUntil loops until context is done, run f every duration given by BackoffManager.

If sliding is true, the period is computed after f runs. If it is false then period includes the runtime for f.

func DecorrelatedJitterDelayHandler added in v0.0.87

func DecorrelatedJitterDelayHandler(_ int, cap, base, last time.Duration) time.Duration

exponentially backed-off with decorrelated jitter sleep = min(cap, random_between(base, sleep * 3))

func EqualJitterDelayHandler added in v0.0.87

func EqualJitterDelayHandler(attempt int, cap, base, _ time.Duration) time.Duration

exponentially backed-off with equal jitter temp = min(cap, base * 2 ** attempt) sleep = temp/2 + random_between(temp/2, min(cap, base * 2 ** attempt))

func ExponentialDelayHandler added in v0.0.87

func ExponentialDelayHandler(attempt int, cap, base, _ time.Duration) time.Duration

exponentially backed-off sleep = min(cap, base * 2 ** attempt)

func Forever added in v0.0.95

func Forever(f func(), period time.Duration)

Forever calls f every period for ever.

Forever is syntactic sugar on top of Until.

func FullJitterDelayHandler added in v0.0.87

func FullJitterDelayHandler(attempt int, cap, base, _ time.Duration) time.Duration

exponentially backed-off with full jitter sleep = random_between(0, min(cap, base * 2 ** attempt))

func Jitter added in v0.0.95

func Jitter(duration time.Duration, maxFactor float64) time.Duration

Jitter returns a time.Duration between [duration - maxFactor*duration, duration + maxFactor*duration].

This allows clients to avoid converging on periodic behavior.

func JitterUntil added in v0.0.95

func JitterUntil(ctx context.Context, f func(ctx context.Context), sliding bool, opts ...ExponentialBackOffOption)

JitterUntil loops until context is done, running f every period.

peroid set by WithExponentialBackOffOptionInitialInterval jitterFactor set by WithExponentialBackOffOptionRandomizationFactor If jitterFactor is positive, the period is jittered before every run of f. If jitterFactor is not positive, the period is unchanged and not jittered.

If sliding is true, the period is computed after f runs. If it is false then period includes the runtime for f.

Cancel context to stop. f may not be invoked if context is already expired.

func NonSlidingUntil added in v0.0.95

func NonSlidingUntil(ctx context.Context, f func(ctx context.Context), period time.Duration)

NonSlidingUntil loops until context is done, running f every period.

NonSlidingUntil is syntactic sugar on top of JitterUntil with zero jitter factor, with sliding = false (meaning the timer for period starts at the same time as the function starts).

func NoneBackOffDelayHandler added in v0.0.87

func NoneBackOffDelayHandler(_ int, cap, base, _ time.Duration) time.Duration

none backed-off sleep = min(cap, base)

func RatioFrom added in v0.0.108

func RatioFrom(from time.Duration, to time.Duration) (ratio time.Duration, divide bool)

Example: ratio, divide := RatioFrom(fromUnit, toUnit)

if divide {
  toDuration = fromDuration / ratio
  fromDuration = toDuration * ratio
} else {
  toDuration = fromDuration * ratio
  fromDuration = toDuration / ratio
}

func UnixWithUnit added in v0.0.108

func UnixWithUnit(timestamp int64, unit time.Duration) time.Time

UnixWithUnit returns the local Time corresponding to the given Unix time by unit

func Until added in v0.0.95

func Until(ctx context.Context, f func(ctx context.Context), period time.Duration)

Until loops until context is done, running f every period.

Until is syntactic sugar on top of JitterUntil with zero jitter factor and with sliding = true (which means the timer for period starts after the f completes).

Types

type BackOff added in v0.0.87

type BackOff interface {
	// Reset to initial state.
	Reset()

	// Gets duration to wait before retrying the operation to
	// indicate that no retries should be made.
	// ok indicates that no more retries should be made.
	// Example usage:
	// var backOffDuration, ok = backoff.NextBackOff();
	// if (!ok) {
	// 	// do not retry operation
	// } else {
	//	// sleep for backOffDuration milliseconds and retry operation
	// }
	NextBackOff() (backoff time.Duration, ok bool)
}

Code borrowed from https://github.com/googleapis/google-http-java-client/blob/master/google-http-client/ src/main/java/com/google/api/client/util/BackOff.java

type Cost added in v0.0.87

type Cost struct {
	// contains filtered or unexported fields
}

func (*Cost) Elapse added in v0.0.87

func (c *Cost) Elapse() time.Duration

func (*Cost) End added in v0.0.87

func (c *Cost) End(f func(d time.Duration))

func (*Cost) Start added in v0.0.87

func (c *Cost) Start()

type Delay

type Delay struct {
	Base    time.Duration
	Cap     time.Duration
	Handler DelayHandler
	// contains filtered or unexported fields
}

func NewDefaultExponentialDelay added in v0.0.87

func NewDefaultExponentialDelay() *Delay

func NewDelay added in v0.0.87

func NewDelay(base, cap time.Duration, h DelayHandler) *Delay

func (*Delay) Delay

func (d *Delay) Delay() <-chan time.Time

func (*Delay) DelayFunc

func (d *Delay) DelayFunc(f func()) *Timer

func (*Delay) NextBackOff added in v0.0.87

func (d *Delay) NextBackOff() (backoff time.Duration, ok bool)

Gets duration to wait before retrying the operation or {@link #STOP} to indicate that no retries should be made.

func (*Delay) Reset

func (d *Delay) Reset()

Reset to initial state.

func (*Delay) Sleep

func (d *Delay) Sleep()

func (*Delay) Update

func (d *Delay) Update()

type DelayHandler added in v0.0.87

type DelayHandler interface {
	Delay(attempt int, cap, base, last time.Duration) (delay time.Duration)
}

type DelayHandlerFunc added in v0.0.87

type DelayHandlerFunc func(attempt int, cap, base, last time.Duration) (delay time.Duration)

The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.

func (DelayHandlerFunc) Delay added in v0.0.87

func (f DelayHandlerFunc) Delay(attempt int, cap, base, last time.Duration) (delay time.Duration)

ServeHTTP calls f(w, r).

type EmptyExponentialBackOffOption added in v0.0.93

type EmptyExponentialBackOffOption struct{}

EmptyExponentialBackOffOption does not alter the configuration. It can be embedded in another structure to build custom options.

This API is EXPERIMENTAL.

type ExponentialBackOff added in v0.0.87

type ExponentialBackOff struct {
	// contains filtered or unexported fields
}

Code borrowed from https://github.com/googleapis/google-http-java-client/blob/master/google-http-client/ src/main/java/com/google/api/client/util/ExponentialBackOff.java

func NewExponentialBackOff added in v0.0.87

func NewExponentialBackOff(opts ...ExponentialBackOffOption) *ExponentialBackOff

func (*ExponentialBackOff) ApplyOptions added in v0.0.93

func (o *ExponentialBackOff) ApplyOptions(options ...ExponentialBackOffOption) *ExponentialBackOff

func (*ExponentialBackOff) GetCurrentInterval added in v0.0.87

func (o *ExponentialBackOff) GetCurrentInterval() time.Duration

Returns the current retry interval.

func (*ExponentialBackOff) GetElapsedDuration added in v0.0.87

func (o *ExponentialBackOff) GetElapsedDuration() time.Duration

Returns the elapsed time since an {@link ExponentialBackOff} instance is created and is reset when {@link #reset()} is called. The elapsed time is computed using {@link System#nanoTime()}.

func (*ExponentialBackOff) GetInitialInterval added in v0.0.87

func (o *ExponentialBackOff) GetInitialInterval() time.Duration

Returns the initial retry interval.

func (*ExponentialBackOff) GetMaxElapsedDuration added in v0.0.87

func (o *ExponentialBackOff) GetMaxElapsedDuration() time.Duration

Returns the maximum elapsed time. If the time elapsed since an {@link ExponentialBackOff} instance is created goes past the max_elapsed_time then the method {@link #NextBackOff()} starts returning STOP. The elapsed time can be reset by calling

func (*ExponentialBackOff) GetMaxInterval added in v0.0.87

func (o *ExponentialBackOff) GetMaxInterval() time.Duration

Returns the maximum value of the back off period. Once the current interval reaches this value it stops increasing.

func (*ExponentialBackOff) GetMultiplier added in v0.0.87

func (o *ExponentialBackOff) GetMultiplier() float64

Returns the value to multiply the current interval with for each retry attempt.

func (*ExponentialBackOff) GetRandomValueFromInterval added in v0.0.87

func (o *ExponentialBackOff) GetRandomValueFromInterval(
	randomizationFactor float64, currentInterval time.Duration) time.Duration

*

  • Returns a random value from the interval [randomizationFactor * currentInterval,
  • randomizationFactor * currentInterval].

func (*ExponentialBackOff) GetRandomizationFactor added in v0.0.87

func (o *ExponentialBackOff) GetRandomizationFactor() float64

Returns the randomization factor to use for creating a range around the retry interval. A randomization factor of 0.5 results in a random period ranging between 50% below and 50% above the retry interval.

func (*ExponentialBackOff) NextBackOff added in v0.0.87

func (o *ExponentialBackOff) NextBackOff() (backoff time.Duration, ok bool)

*

  • {@inheritDoc} *
  • <p>This method calculates the next back off interval using the formula: randomized_interval =
  • retry_interval +/- (randomization_factor * retry_interval) *
  • <p>Subclasses may override if a different algorithm is required.

func (*ExponentialBackOff) Reset added in v0.0.87

func (o *ExponentialBackOff) Reset()

Sets the interval back to the initial retry interval and restarts the timer.

type ExponentialBackOffOption added in v0.0.93

type ExponentialBackOffOption interface {
	// contains filtered or unexported methods
}

A ExponentialBackOffOption sets options.

func WithExponentialBackOffOptionInitialInterval added in v0.0.93

func WithExponentialBackOffOptionInitialInterval(duration time.Duration) ExponentialBackOffOption

func WithExponentialBackOffOptionMaxElapsedDuration added in v0.0.93

func WithExponentialBackOffOptionMaxElapsedDuration(duration time.Duration) ExponentialBackOffOption

func WithExponentialBackOffOptionMaxInterval added in v0.0.93

func WithExponentialBackOffOptionMaxInterval(maxInterval time.Duration) ExponentialBackOffOption

func WithExponentialBackOffOptionMultiplier added in v0.0.93

func WithExponentialBackOffOptionMultiplier(multiplier float64) ExponentialBackOffOption

func WithExponentialBackOffOptionRandomizationFactor added in v0.0.93

func WithExponentialBackOffOptionRandomizationFactor(factor float64) ExponentialBackOffOption

type ExponentialBackOffOptionFunc added in v0.0.93

type ExponentialBackOffOptionFunc func(*ExponentialBackOff)

ExponentialBackOffOptionFunc wraps a function that modifies ExponentialBackOff into an implementation of the ExponentialBackOffOption interface.

type StopBackOff added in v0.0.87

type StopBackOff struct {
}

func (*StopBackOff) NextBackOff added in v0.0.87

func (o *StopBackOff) NextBackOff() (backoff time.Duration, ok bool)

func (*StopBackOff) Reset added in v0.0.87

func (o *StopBackOff) Reset()

type Timer

type Timer struct {
	*time.Timer
}

https://github.com/golang/go/issues/27169 Timer to fix time: Timer.Stop documentation example easily leads to deadlocks

func AfterFunc

func AfterFunc(d time.Duration, f func()) *Timer

func NewTimer

func NewTimer(d time.Duration) *Timer

func WrapTimer

func WrapTimer(t *time.Timer) *Timer

func (*Timer) Reset

func (t *Timer) Reset(d time.Duration) bool

Reset changes the timer to expire after duration d. Reset can be invoked anytime, which enhances std time.Reset Reset == std [Stop + drain + Reset] It returns true if the timer had been active, false if the timer had expired or been stopped.

func (*Timer) Stop

func (t *Timer) Stop() bool

Stop prevents the Timer from firing, with the channel drained. Stop ensures the channel is empty after a call to Stop. Stop == std [Stop + drain] It returns true if the call stops the timer, false if the timer has already expired or been stopped. Stop does not close the channel, to prevent a read from the channel succeeding incorrectly.

type UnixSecondTime added in v0.0.108

type UnixSecondTime struct {
	time.Time
}

func (UnixSecondTime) MarshalJSON added in v0.0.108

func (t UnixSecondTime) MarshalJSON() ([]byte, error)

func (UnixSecondTime) MarshalText added in v0.0.108

func (t UnixSecondTime) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The time is formatted in Unix Seconds, with sub-second precision added if present.

func (UnixSecondTime) String added in v0.0.108

func (t UnixSecondTime) String() string

func (*UnixSecondTime) UnmarshalJSON added in v0.0.108

func (t *UnixSecondTime) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339 format.

func (*UnixSecondTime) UnmarshalText added in v0.0.108

func (t *UnixSecondTime) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to be in RFC 3339 format.

type UnixTime added in v0.0.108

type UnixTime = UnixSecondTime

type UnixTimeDay added in v0.0.108

type UnixTimeDay struct {
	time.Time
}

func (UnixTimeDay) MarshalJSON added in v0.0.108

func (t UnixTimeDay) MarshalJSON() ([]byte, error)

func (UnixTimeDay) MarshalText added in v0.0.108

func (t UnixTimeDay) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The time is formatted in Unix Seconds, with sub-second precision added if present.

func (UnixTimeDay) String added in v0.0.108

func (t UnixTimeDay) String() string

func (*UnixTimeDay) UnmarshalJSON added in v0.0.108

func (t *UnixTimeDay) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339 format.

func (*UnixTimeDay) UnmarshalText added in v0.0.108

func (t *UnixTimeDay) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to be in RFC 3339 format.

type UnixTimeHour added in v0.0.108

type UnixTimeHour struct {
	time.Time
}

func (UnixTimeHour) MarshalJSON added in v0.0.108

func (t UnixTimeHour) MarshalJSON() ([]byte, error)

func (UnixTimeHour) MarshalText added in v0.0.108

func (t UnixTimeHour) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The time is formatted in Unix Seconds, with sub-second precision added if present.

func (UnixTimeHour) String added in v0.0.108

func (t UnixTimeHour) String() string

func (*UnixTimeHour) UnmarshalJSON added in v0.0.108

func (t *UnixTimeHour) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339 format.

func (*UnixTimeHour) UnmarshalText added in v0.0.108

func (t *UnixTimeHour) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to be in RFC 3339 format.

type UnixTimeMicrosecond added in v0.0.108

type UnixTimeMicrosecond struct {
	time.Time
}

func (UnixTimeMicrosecond) MarshalJSON added in v0.0.108

func (t UnixTimeMicrosecond) MarshalJSON() ([]byte, error)

func (UnixTimeMicrosecond) MarshalText added in v0.0.108

func (t UnixTimeMicrosecond) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The time is formatted in Unix Seconds, with sub-second precision added if present.

func (UnixTimeMicrosecond) String added in v0.0.108

func (t UnixTimeMicrosecond) String() string

func (*UnixTimeMicrosecond) UnmarshalJSON added in v0.0.108

func (t *UnixTimeMicrosecond) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339 format.

func (*UnixTimeMicrosecond) UnmarshalText added in v0.0.108

func (t *UnixTimeMicrosecond) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to be in RFC 3339 format.

type UnixTimeMillisecond added in v0.0.108

type UnixTimeMillisecond struct {
	time.Time
}

func (UnixTimeMillisecond) MarshalJSON added in v0.0.108

func (t UnixTimeMillisecond) MarshalJSON() ([]byte, error)

func (UnixTimeMillisecond) MarshalText added in v0.0.108

func (t UnixTimeMillisecond) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The time is formatted in Unix Seconds, with sub-second precision added if present.

func (UnixTimeMillisecond) String added in v0.0.108

func (t UnixTimeMillisecond) String() string

func (*UnixTimeMillisecond) UnmarshalJSON added in v0.0.108

func (t *UnixTimeMillisecond) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339 format.

func (*UnixTimeMillisecond) UnmarshalText added in v0.0.108

func (t *UnixTimeMillisecond) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to be in RFC 3339 format.

type UnixTimeMinute added in v0.0.108

type UnixTimeMinute struct {
	time.Time
}

func (UnixTimeMinute) MarshalJSON added in v0.0.108

func (t UnixTimeMinute) MarshalJSON() ([]byte, error)

func (UnixTimeMinute) MarshalText added in v0.0.108

func (t UnixTimeMinute) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The time is formatted in Unix Seconds, with sub-second precision added if present.

func (UnixTimeMinute) String added in v0.0.108

func (t UnixTimeMinute) String() string

func (*UnixTimeMinute) UnmarshalJSON added in v0.0.108

func (t *UnixTimeMinute) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339 format.

func (*UnixTimeMinute) UnmarshalText added in v0.0.108

func (t *UnixTimeMinute) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to be in RFC 3339 format.

type UnixTimeNanosecond added in v0.0.108

type UnixTimeNanosecond struct {
	time.Time
}

func (UnixTimeNanosecond) MarshalJSON added in v0.0.108

func (t UnixTimeNanosecond) MarshalJSON() ([]byte, error)

func (UnixTimeNanosecond) MarshalText added in v0.0.108

func (t UnixTimeNanosecond) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The time is formatted in Unix Seconds, with sub-second precision added if present.

func (UnixTimeNanosecond) String added in v0.0.108

func (t UnixTimeNanosecond) String() string

func (*UnixTimeNanosecond) UnmarshalJSON added in v0.0.108

func (t *UnixTimeNanosecond) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface. The time is expected to be a quoted string in RFC 3339 format.

func (*UnixTimeNanosecond) UnmarshalText added in v0.0.108

func (t *UnixTimeNanosecond) UnmarshalText(data []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface. The time is expected to be in RFC 3339 format.

type ZeroBackOff added in v0.0.87

type ZeroBackOff struct {
}

func (*ZeroBackOff) NextBackOff added in v0.0.87

func (o *ZeroBackOff) NextBackOff() (backoff time.Duration, ok bool)

func (*ZeroBackOff) Reset added in v0.0.87

func (o *ZeroBackOff) Reset()

Directories

Path Synopsis
The key observation and some code (shr) is borrowed from golang.org/x/time/rate/rate.go
The key observation and some code (shr) is borrowed from golang.org/x/time/rate/rate.go

Jump to

Keyboard shortcuts

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