timed

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2024 License: Apache-2.0 Imports: 4 Imported by: 51

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Periodic

func Periodic(ctx canceler, period time.Duration, fn func() error) error

Periodic executes fn on every period. Periodic returns if the context is cancelled. The underlying ticket adjusts the intervals or drops ticks to make up for slow runs of fn. If fn is active, Periodic will only return when fn has finished. The period must be greater than 0, otherwise Periodic panics.

If fn returns an Error, then the loop is stopped and the functions error is returned directly. On normal termination the contexts reported error will be reported.

func RetryUntil added in v0.3.0

func RetryUntil(ctx canceler, timeout, period time.Duration, fn func(canceler) error) error

RetryUntil executes fn periodically until the function no longer returns an error, or the timeout has elapsed, or the context is canceled. If the timeout has elapsed and fn still returns an error, RetryUntil wraps the original error from fn and returns it. If fn no longer returns an error, RetryUntil returns nil.

Example:

err := RetryUntil(context.Background(), 1 * time.Second, 10 * time.Millisecond, func(ctx context.Contect) error {
    actual := getCount(ctx)
    if actual != 3 {
        return fmt.Errorf("expected 3 items, got %d", actual)
    }
})
if err != nil {
    fmt.Printf("RetryUntil has failed: %+v\n", err)
} else {
    fmt.Println("good things come to those who wait")
}

func Wait

func Wait(ctx canceler, duration time.Duration) error

Wait blocks for the configuration duration or until the passed context signal canceling. Wait return ctx.Err() if the context got cancelled early. If the duration has passed without the context being cancelled, Wait returns nil.

Example:

fmt.Printf("wait for 5 seconds...")
if err := Wait(ctx, 5 * time.Second); err != nil {
    fmt.Printf("shutting down")
    return err
}
fmt.Println("done")

Types

This section is empty.

Jump to

Keyboard shortcuts

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