Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewNonRetryable ¶ added in v0.0.5
NewNonRetryable initializes a non retryable error.
Types ¶
type Backoffer ¶
type Backoffer interface {
Backoff(fn func() error) error
BackoffCtx(ctx context.Context, fn func(context.Context) error) error
}
Backoffer is an interface to abstract the Runner type into just a the backoff call.
type Counter ¶
type Counter interface {
Inc()
}
Counter is a metric that will be incremented if an error is encountered.
type NonRetryableError ¶ added in v0.0.5
type NonRetryableError struct {
// contains filtered or unexported fields
}
NonRetryableError wraps an err to add a Retry() method that conforms to Retrier interface which then prevents a backoff runner from retrying.
func (NonRetryableError) Error ¶ added in v0.0.5
func (e NonRetryableError) Error() string
Error returns the error as a string.
func (NonRetryableError) Retry ¶ added in v0.0.5
func (e NonRetryableError) Retry() bool
Retry tells the backoff not to retry this error.
func (NonRetryableError) Unwrap ¶ added in v0.0.5
func (e NonRetryableError) Unwrap() error
Unwrap returns the underlying wrapped error.
type NoopBackoff ¶
type NoopBackoff struct{}
NoopBackoff is a backoff that is a noop for the backoff. Only calls the func provided to the backoff calls.
func (NoopBackoff) Backoff ¶
func (n NoopBackoff) Backoff(fn func() error) error
Backoff is a backoff runner.
func (NoopBackoff) BackoffCtx ¶
BackoffCtx is a backoff runner that may be interrupted via the provided context.
type Retrier ¶
type Retrier interface {
Retry() bool
}
Retrier determines if an error exhibits the behavior of an error that is safe to retry. Useful if you don't want some types of errors to be retried (ie. something that causes an HTTP 400 is likely to be successful on a subsequent request if the request is unchanged).
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner is a backoff runner that will run a backoff func, safe for concurrent use, and provides DI for a backoff callsites.
func New ¶
func New(opts ...RunnerOptFn) Runner
New returns a runner with the defined options. If no options are given, then the new runner is given the defaults for initial and max backoff as well as max calls. The defaults being:
InitBackoff: 1 second MaxBackoff: 1 minute MaxCalls: 10 Jitter: false
Example (Customized) ¶
package main
import (
"errors"
"fmt"
"log"
"time"
"github.com/jasonhancock/go-backoff"
)
type myLogger struct{}
func (l *myLogger) Warn(msg any, keyvals ...any) {
fmt.Println(msg)
}
func main() {
boff := backoff.New(
backoff.WithLogger(&myLogger{}),
backoff.InitBackoff(10*time.Second),
backoff.MaxCalls(2),
)
i := 0
err := boff.Backoff(func() error {
i++
if i < 4 {
return errors.New("some error")
}
return nil
})
if err != nil {
log.Fatal(err)
}
}
Output:
Example (Minimum) ¶
package main
import (
"errors"
"fmt"
"log"
"github.com/jasonhancock/go-backoff"
)
type myLogger struct{}
func (l *myLogger) Warn(msg any, keyvals ...any) {
fmt.Println(msg)
}
func main() {
boff := backoff.New(backoff.WithLogger(&myLogger{}))
i := 0
err := boff.Backoff(func() error {
i++
if i < 4 {
return errors.New("some error")
}
return nil
})
if err != nil {
log.Fatal(err)
}
}
Output:
func (Runner) BackoffCtx ¶
BackoffCtx runs the given func in a backoff loop defined by the runner type.
func (Runner) New ¶
func (r Runner) New(opts ...RunnerOptFn) Runner
New allows one to create a new runner, with any options, from an existing runner type.
type RunnerOptFn ¶
RunnerOptFn is a functional option to set the
func InitBackoff ¶
func InitBackoff(i time.Duration) RunnerOptFn
InitBackoff sets the runners initial backoff time.
func Jitter ¶
func Jitter() RunnerOptFn
Jitter sets the Backoff method to jitter the backoff duration.
func MaxBackoff ¶
func MaxBackoff(m time.Duration) RunnerOptFn
MaxBackoff sets the runners max backoff time.
func WithCounter ¶
func WithCounter(c Counter) RunnerOptFn
WithCounter injects a counter that will be incremented when a backoff needs to be performed.
func WithLogger ¶
func WithLogger(l Logger) RunnerOptFn
WithLogger sets the Logger on the Runner type.