Skip to content

Latest commit

 

History

History
84 lines (69 loc) · 1.82 KB

README.md

File metadata and controls

84 lines (69 loc) · 1.82 KB

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.