Skip to content

Latest commit

 

History

History
64 lines (54 loc) · 1.04 KB

README.md

File metadata and controls

64 lines (54 loc) · 1.04 KB

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 [email protected]