Code Monkey home page Code Monkey logo

go-wait's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.