Skip to content

Golang simple backoff and jitter implementations for retrying operations

License

Notifications You must be signed in to change notification settings

brunotm/backoff

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 

Repository files navigation

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

About

Golang simple backoff and jitter implementations for retrying operations

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages