Categorygithub.com/go-playground/backoff-sys
modulepackage
1.3.0
Repository: https://github.com/go-playground/backoff-sys.git
Documentation: pkg.go.dev

# 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.

# Functions

NewExponential create a new exponential backoff builder with sane defaults.

# Variables

No description provided by the author

# Structs

Exponential is the final read-only(thread safe) backoff entity.
ExponentialBuilder helps to build the final exponential backoff entity.