backoff

package module
v0.0.0-...-49b03d1 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

README

backoff

backoff provides a simple backoff and jitter implementation for retrying operations
As described in:
Bounded retries:
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/brunotm/backoff"
)

func main() {
	count := 0
	err := backoff.Retry(
		context.Background(), 100, 1*time.Second, 60*time.Second,
		func() error {
			count++
			fmt.Println("Count: ", count)
			if count == 5 {
				return nil
			}
			return fmt.Errorf("op error")
		})
	fmt.Println(err)
}
Unbounded retries:
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/brunotm/backoff"
)

func main() {
	count := 0
	// Until only returns an error when the context is done.
	err := backoff.Until(
		context.Background(), 1*time.Second, 60*time.Second,
		func() error {
			count++
			fmt.Println("Count: ", count)
			if count == 5 {
				return nil
			}
			return fmt.Errorf("op error")
		})
	fmt.Println(err)
}

Written by Bruno Moura brunotm@gmail.com

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrContextDone returned when context is canceled
	ErrContextDone = errors.New("context done")
)

Functions

func Retry

func Retry(ctx context.Context, attempts int, base, max time.Duration, f func() error) (err error)

Retry the given function n times jittering between max and min time.Duration

func Until

func Until(ctx context.Context, base, max time.Duration, f func() error) (err error)

Until is like retry but retries until success

Types

This section is empty.

Jump to

Keyboard shortcuts

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