Documentation ¶
Index ¶
- Variables
- func IsRecoverable(err error) bool
- func Unrecoverable(err error) error
- func WithoutCancel(ctx context.Context) context.Context
- type AsyncRetry
- type Config
- type FinishFunc
- type OnRetryFunc
- type Option
- func Attempts(attempts uint) Option
- func CancelWhenConfigContextCanceled(cancel bool) Option
- func CancelWhenShutdown(cancel bool) Option
- func Context(ctx context.Context) Option
- func Delay(delay time.Duration) Option
- func MaxJitter(maxJitter time.Duration) Option
- func OnRetry(onRetry OnRetryFunc) Option
- func RetryIf(retryIf RetryIfFunc) Option
- func Timeout(timeout time.Duration) Option
- type RetryIfFunc
- type RetryableFunc
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var DefaultConfig = Config{ // contains filtered or unexported fields }
var ErrInShutdown = fmt.Errorf("AsyncRetry is in shutdown")
Functions ¶
func IsRecoverable ¶
IsRecoverable checks if error is recoverable
Types ¶
type AsyncRetry ¶
type AsyncRetry interface { // Do calls f in a new goroutine, and retry if necessary. When finished, `finish` is called regardless of success or failure exactly once. // Non-nil error is always `ErrInShutdown` that will be returned when AsyncRetry is in shutdown. Do(ctx context.Context, f RetryableFunc, finish FinishFunc, opts ...Option) error // Shutdown gracefully shuts down AsyncRetry without interrupting any active `Do`. // Shutdown works by first stopping to accept new `Do` request, and then waiting for all active `Do`'s background goroutines to be finished. // Multiple call of Shutdown is OK. Shutdown(ctx context.Context) error }
Example ¶
package main import ( "context" "fmt" "log" "net/http" "os" "os/signal" "time" asyncretry "github.com/Kyash/async-retry" ) func main() { mux := http.NewServeMux() asyncRetry := asyncretry.NewAsyncRetry() mux.HandleFunc( "/hello", func(w http.ResponseWriter, r *http.Request) { var ctx = r.Context() if err := asyncRetry.Do( ctx, func(ctx context.Context) error { // do task // ... return nil }, func(err error) { if err != nil { log.Println(err.Error()) } }, asyncretry.Attempts(5), asyncretry.Timeout(8*time.Second), ); err != nil { // asyncRetry is in shutdown log.Println(err.Error()) } fmt.Fprintf(w, "Hello") }, ) srv := &http.Server{Handler: mux} ch := make(chan struct{}) go func() { sigint := make(chan os.Signal, 1) signal.Notify(sigint, os.Interrupt) <-sigint if err := srv.Shutdown(context.Background()); err != nil { log.Printf("HTTP server Shutdown: %v", err) } if err := asyncRetry.Shutdown(context.Background()); err != nil { log.Printf("AsyncRetry Shutdown: %v", err) } close(ch) }() if err := srv.ListenAndServe(); err != http.ErrServerClosed { log.Fatalf("HTTP server ListenAndServe: %v", err) } <-ch }
Output:
func NewAsyncRetry ¶
func NewAsyncRetry() AsyncRetry
type FinishFunc ¶
type FinishFunc func(error)
type OnRetryFunc ¶
OnRetryFunc is a signature of OnRetry function n = 0-indexed count of attempts.
type Option ¶
type Option func(*Config)
Option represents an option for async-retry.
func Attempts ¶
Attempts set count of retry If set 0 or 1, only initial call is permitted If set 2, initial call and one retry are permitted Note: retry-go supports 0 which means infinity, but we don't support that
func CancelWhenConfigContextCanceled ¶
CancelWhenConfigContextCanceled sets boolean If true, cancel the context specified as an argument of RetryableFunc when c.context is canceled
func CancelWhenShutdown ¶
CancelWhenShutdown sets boolean If true, cancel the context specified as an argument of RetryableFunc when Shutdown is called
func Delay ¶
Delay sets delay between retry Interval between retry equals to the sum of c.delay << n and random number in the half-open interval [0,c.maxJitter)
func RetryIf ¶
func RetryIf(retryIf RetryIfFunc) Option
RetryIf sets RetryIfFunc If retryIf returns false, retry will be stopped
type RetryIfFunc ¶
RetryIfFunc is s signature of retry if function