Documentation
¶
Overview ¶
Package retry allows for abstracted retry mechanism to hide the gory details of what is happening underneath.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AbortedRetries ¶ added in v1.0.0
AbortedRetries wraps the message passed and returns an error that be read by the error handler within the retry client.
func ExceededRetries ¶ added in v1.0.0
ExceededRetries wraps the message passed and returns an error that be read by the error handler within the retry client.
func HasAborted ¶ added in v1.0.0
HasAborted checks the error to validate if the executed function has notified that it should abort now.
func HasExceeded ¶ added in v1.0.0
HasExceeded checks error to validate if the exectued function has notified that it exceeded the limit.
Types ¶
type Option ¶
type Option func(r *retry) error
Option allows for additional functionality to be added to the Retryer on creation
func WithExponentialBackoff ¶
WithExponentialBackoff will start from a fixed delay and increase the delay amount by increasing it by the multiplier amount
func WithFixedDelay ¶
WithFixedDelay will set the delay experienced after each failed attempted
func WithJitter ¶
WithJitter generates a random sleep interval between [0, delay) to help spread retry attemps over different intervals
type Retryer ¶
type Retryer interface {
// Do will execute the function until it has reached the permissable limit
// that is passed. If the function was to return nil, the retry will exit early
// otherwise, the error is passed to an error handler and the next attempt is
// started after the post execution function have run.
// If the attempt limit has been reached, ErrAttemptsExceeded is returned.
Do(limit int, f func() error) error
// DoWithContext extendes the Do method by ensuring that any attempts are
// aborted if the passed context is done.
DoWithContext(ctx context.Context, limit int, f func() error) error
}
Retryer abstracts the retry functionality of executing a function
func Must ¶
Must is a convenience function for New to avoid having to handle the error and allow for inline creation. If an error was to be returned from the wrapped New function, it would cause this function to panic instead.
Example ¶
package main
import (
"errors"
"fmt"
"time"
"github.com/MovieStoreGuy/retry"
)
func main() {
r := retry.Must(
retry.WithFixedDelay(100*time.Millisecond), // Ensures each failed attempt waits 100ms
retry.WithJitter(10*time.Millisecond), // Ensures each failed attempt waits at most 10ms
)
_ = r.Do(3, func() error {
fmt.Print(`tick...`)
return errors.New(`boom`)
})
}
Output: tick...tick...tick...
func New ¶
New creates a new retry with the configured options provided. An error is returned if any of the options failed to apply
Example ¶
package main
import (
"fmt"
"github.com/MovieStoreGuy/retry"
)
func main() {
r, err := retry.New()
if err != nil {
panic(err)
}
err = r.Do(4, func() error {
fmt.Println(`Woohoo`)
return nil
})
if err != nil {
panic(err)
}
}
Output: Woohoo