backoff

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: May 9, 2023 License: MIT Imports: 5 Imported by: 4

README

Package backoff-sys

Project status Actions Status GoDoc License

Package backoff-sys provides the bare building blocks for backing off and can be used to build more complex backoff packages, but this is likely enough. This includes:

  • Exponential backoff, with jitter
  • Linear backoff, with jitter

Example

// go run _examples/exponential/main.go
package main

import (
	"errors"
	"fmt"
	"time"

	"github.com/go-playground/backoff-sys"
)

func main() {
	bo := backoff.NewExponential().Init()
	for i := 0; i < 5; i++ {
		err := fallible()
		if err != nil {
			d := bo.Duration(i)
			fmt.Printf("Waiting: %s\n", d)
			time.Sleep(d)
			continue
		}
	}
}

func fallible() error {
	return errors.New("failed")
}

or with cancelable sleep helper

package main

import (
	"context"
	"errors"
	"fmt"
	"time"

	"github.com/go-playground/backoff-sys"
)

func main() {
	bo := backoff.NewExponential().Init()
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	for i := 0; i < 5; i++ {
		err := fallible()
		if err != nil {
			start := time.Now()
			if err := bo.Sleep(ctx, i); err != nil {
				panic(err)
			}
			fmt.Printf("Waited %s\n", time.Since(start))
			continue
		}
	}
}

func fallible() error {
	return errors.New("failed")
}

License

Distributed under MIT License, please see license file in code for more details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrMaxAttemptsReached = errorsext.ErrMaxAttemptsReached

Functions

This section is empty.

Types

type Exponential

type Exponential struct {
	// contains filtered or unexported fields
}

Exponential is the final read-only(thread safe) backoff entity

func (Exponential) Duration

func (e Exponential) Duration(attempt int) time.Duration

Duration accepts attempt and returns the backoff duration o sleep for.

func (Exponential) Sleep added in v1.1.0

func (e Exponential) Sleep(ctx context.Context, attempt int) error

Sleep is a convenience function wrapping Duration and allowing the sleep time to be cancelled via the Context.

This function can also return ErrMaxAttemptsReached if the max attempts have been reached.

type ExponentialBuilder

type ExponentialBuilder struct {
	// contains filtered or unexported fields
}

ExponentialBuilder helps to build the final exponential backoff entity

func NewExponential

func NewExponential() ExponentialBuilder

NewExponential create a new exponential backoff builder with sane defaults.

func (ExponentialBuilder) Factor

Factor sets a factor for the backoff algorithm.

func (ExponentialBuilder) Init

Init returns a read-only(thread safe) Exponential backoff entity for use.

func (ExponentialBuilder) Interval

func (e ExponentialBuilder) Interval(interval time.Duration) ExponentialBuilder

Interval sets base wait interval for the backoff algorithm.

func (ExponentialBuilder) Jitter

Jitter sets the maximum jitter for the backoff algorithm.

func (ExponentialBuilder) Max added in v1.1.0

Max sets the maximum timeout despite the number of attempts. none/zero is the default.

func (ExponentialBuilder) MaxAttempts added in v1.2.0

func (e ExponentialBuilder) MaxAttempts(max int) ExponentialBuilder

MaxAttempts sets the maximum number of attempts before the Sleep(...) function begins returning ErrMaxAttemptsReached, by default is unlimited.

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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