wait

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2021 License: Apache-2.0 Imports: 4 Imported by: 4

README

go-wait

Tests

A tiny util library for programs that require a little patience ⏰

Usage

Wait

Useful for simple cases where a predictable bool check should be ran in a set interval and continue when it is satisfied.

Basic usage
import (
	wait "github.com/asaf-shitrit/go-wait"
)

checkFunc := func() (bool, error) {
    // any bool based logic that changes over a 
    // given period of time
}

ctx := context.Background() // or pass any ctx you would like
if err := wait.Until(ctx, checkFunc); err != nil {
    // handle logical/timeout err
}

// logic that should happen after check is satisfied
With explicit options
import (
	wait "github.com/asaf-shitrit/go-wait"
)

checkFunc := func() (bool, error) {
    // any bool based logic that changes over a 
    // given period of time
}

options := &wait.UntilOptions{
    Timeout: time.Minute
    Interval: time.Second
}

ctx := context.Background() // or pass any ctx you would like
if err := wait.Until(ctx, checkFunc, options); err != nil {
    // handle logical/timeout err
}

// logic that should happen after check is satisfied
Backoff

Really useful in cases that low CPU overhead a constraint and the check intervals should be backed off after each run.

It was inspired by Go's own http.Server Shutdown implementation ❤️

Basic usage
import (
	wait "github.com/asaf-shitrit/go-wait"
)

checkFunc := func() (bool, error) {
    // any bool based logic that changes over a 
    // given period of time
}

ctx := context.Background() // or pass any ctx you would like
if err := wait.Backoff(ctx, checkFunc); err != nil {
    // handle logical/timeout err
}

// logic that should happen after check is satisfied
With explicit options
import (
	wait "github.com/asaf-shitrit/go-wait"
)

checkFunc := func() (bool, error) {
    // any bool based logic that changes over a 
    // given period of time
}

options := &wait.BackoffOptions{
	BaselineDuration: time.Millisecond,
	Limit:            500 * time.Millisecond,
	Multiplier:       2,
}

ctx := context.Background() // or pass any ctx you would like
if err := wait.Backoff(ctx, checkFunc, options); err != nil {
    // handle logical/timeout err
}

// logic that should happen after check is satisfied
Capabilities
Timeout & Cancel ⏰

It is aligned with Golang concept of context so explicit cancels & timeout will work out of the box.

Jitter

Allows you to set an amount of jitter percentage that will apply for the calculation of each interval.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Backoff

func Backoff(ctx context.Context, check func() (bool, error), o ...*BackoffOptions) error

Backoff is a waiting mechanism that allows for better CPU load as the interval starts from a given baseline and then backs off until it reaches the provided limit.

Note: this is partially bases off of http.Server implementation of their Shutdown polling mechanism.

func Until

func Until(ctx context.Context, check func() (bool, error), o ...*UntilOptions) error

Until allows for a predictable interval based waiting mechanism until the given bool based check is satisfied.

Types

type BackoffOptions

type BackoffOptions struct {
	Jitter                  int
	BaselineDuration, Limit time.Duration
	Multiplier              int64
}

type UntilOptions

type UntilOptions struct {
	Interval time.Duration
	Jitter   int
}

Jump to

Keyboard shortcuts

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