run

package
v0.0.0-...-6b6864c Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Do

func Do(parent context.Context, fn func(context.Context) (bool, error), ro RetryOptions) error

func Every

func Every[T Interval](ctx context.Context, fn func(), interval T)

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

func Unless[R RetryFunc, T Interval](ctx context.Context, fn R, interval T) error

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

func Until[R RetryFunc, T Interval](ctx context.Context, fn R, interval T) error

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 RetryFunc

type RetryFunc interface {
	func() bool | func() error | func() (bool, error)
}

type RetryIterator

type RetryIterator iter.Seq2[int, error]

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) RangeE

func (r RetryIterator) RangeE(fn func(int, error) error) 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()

Jump to

Keyboard shortcuts

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