Documentation ¶
Overview ¶
Package retry allows to use retry mechanism (call some function until it does not return non-error result or attempts count is not exceeded).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func Do ¶
Do executes passed function and repeat it until non-error returned or maximum retries attempts count in not exceeded. Attempts counter starts from 1.
Example ¶
var ( content []byte ctx = context.Background() ) err := Do(func(uint) error { req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://httpbin.org/anything", nil) if err != nil { return err } resp, err := http.DefaultClient.Do(req) if err != nil { return err } defer func() { _ = resp.Body.Close() }() content, err = ioutil.ReadAll(resp.Body) if err != nil { return err } return nil }, WithContext(ctx), WithAttempts(10), WithDelay(time.Second), WithLastErrorReturning()) if err != nil { panic(err) } fmt.Printf("Response: %s", string(content))
Output:
Types ¶
type Option ¶
type Option func(*config)
func WithAttempts ¶
WithAttempts overrides default attempts count.
func WithContext ¶
WithContext overrides default context (by default `context.Background()` is used).
func WithLastErrorReturning ¶
func WithLastErrorReturning() Option
WithLastErrorReturning allows to return last execution error (instead ErrToManyAttempts or context error).
func WithRetryStoppingErrors ¶
WithRetryStoppingErrors allows to thor the retry loop when defined error returned from calling function.
Click to show internal directories.
Click to hide internal directories.