Documentation
¶
Index ¶
- func Do(parent context.Context, fn func(context.Context) (bool, error), ...) error
- func Every[T Interval](ctx context.Context, fn func(), interval T)
- func Unless[R RetryFunc, T Interval](ctx context.Context, fn R, interval T) error
- func Until[R RetryFunc, T Interval](ctx context.Context, fn R, interval T) error
- type Interval
- type RetryFunc
- type RetryIterator
- type RetryOptions
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Every ¶
Every runs a function periodically for the provided interval. It runs indefinitely or until the context is done.
The interval can be either a time.Duration or, if more complex retry logic is required, a RetryOptions.
Example ¶
package main
import (
"context"
"fmt"
"time"
"go.chrisrx.dev/x/run"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
run.Every(ctx, func() {
fmt.Println("doing some work")
}, 450*time.Millisecond)
}
Output: doing some work doing some work doing some work doing some work doing some work
func Unless ¶
Unless runs a function periodically for the provided interval. This is used for running logic until something is unsuccessful. It is the inverse of Until.
Unless has different behaviors depending on the retry function that is passed in. If the function returns an error, it will run until an error is encountered. If given a retry function returning a bool, then it will run until false is returned.
The interval can be either a time.Duration or, if more complex retry logic is required, a RetryOptions.
func Until ¶
Until runs a function periodically for the provided interval. This is used for running logic until something is successful or until the context is done.
Until has different behaviors depending on the retry function that is passed in. If the function returns an error, it will run until no error is encountered. If given a retry function returning a bool, then it will run until true is returned.
The interval can be either a time.Duration or, if more complex retry logic is required, a RetryOptions.
Types ¶
type Interval ¶
type Interval interface {
time.Duration | RetryOptions
}
type RetryIterator ¶
func Retry
deprecated
func Retry(ctx context.Context, fn func() error, ro RetryOptions) RetryIterator
Retry runs a function periodically based on the provided [Options].
Deprecated: I don't like it
func (RetryIterator) Range ¶
func (r RetryIterator) Range(fn func(int, error))
func (RetryIterator) Wait ¶
func (r RetryIterator) Wait()
Wait ranges over the iterator until no more elements are produced. The last error is ignored.
func (RetryIterator) WaitE ¶
func (r RetryIterator) WaitE() error
Wait ranges over the iterator until no more elements are produced. If an error is encountered, the last error received will be returned.
type RetryOptions ¶
type RetryOptions struct {
InitialInterval time.Duration
MaxAttempts int
MaxAttemptTime time.Duration
MaxElapsedTime time.Duration
MaxInterval time.Duration
Multiplier float64
RandomizationFactor float64
// contains filtered or unexported fields
}
func DefaultRetryOptions ¶
func DefaultRetryOptions() RetryOptions
func (*RetryOptions) Backoff ¶
func (ro *RetryOptions) Backoff() backoff.Backoff
func (*RetryOptions) Reset ¶
func (ro *RetryOptions) Reset()