wait

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2025 License: MIT Imports: 4 Imported by: 1

README

wait - wait for a condition to become true

GoDoc unit tests report card codecov

Install:

go get github.com/singlestore-labs/wait

Wait provides one function: For.

	err := wait.For(func() (bool, error) {
		err := trySomething()
		if err != nil {
			if isFatal(err) {
				return false, err
			}
			return false, nil
		}
		return true, nil
	})

It calls its function argument over and over until:

  • a timeout occurs (failure)
  • the bool becomes true (success or failure depending on error)
  • if ExitOnError(true) and the error becomes non nil (failure)

Documentation

Index

Constants

View Source
const ErrTimeout errors.String = "timeout"

Variables

This section is empty.

Functions

func For

func For(f func() (bool, error), options ...Option) error

For calls a function repeatedly. It will stop calling and return ErrTimeout if too much time has passed. Otherwise it stops when the function returns true. If the function returns (true, error), then For() returns that error. An error return from function parameter does not cause the loop to exit unless ExitOnError(true) is is set.

Types

type Logger

type Logger func(fmt string, args ...any)

type O

type O struct {
	TimeLimit     time.Duration
	StartInterval time.Duration
	MaxInterval   time.Duration
	Logger        Logger
	Backoff       float64
	Reports       int
	Reporter      Reporter
	Description   string
	Ctx           context.Context
	ExitOnError   bool
}

type Option

type Option func(*O)

func ExitOnError

func ExitOnError(exit bool) Option

func WithBackoff

func WithBackoff(f float64) Option

WithBackoff sets how much the interval should change after the function is called. Reasonable values are in the range 1.01 to 1.04. The default is 1.02. Do not set this to a value that is < 1.0.

func WithContext

func WithContext(ctx context.Context) Option

func WithDescription

func WithDescription(s string) Option

func WithInterval

func WithInterval(d time.Duration) Option

WithInterval sets both the minimum and maximum intervals

func WithLimit

func WithLimit(d time.Duration) Option

WithLimit sets the maximum total time to try

func WithLogger

func WithLogger(f Logger) Option

func WithMaxInterval

func WithMaxInterval(d time.Duration) Option

func WithMinInterval

func WithMinInterval(d time.Duration) Option

func WithReporter

func WithReporter(f Reporter) Option

func WithReports

func WithReports(n int) Option

WithReports specifies that there be approximately N progress reports before timeout. Values must be 0 and above.

type Reporter

type Reporter func(opts O, startTime time.Time)

Jump to

Keyboard shortcuts

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