Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DoValue ¶ added in v1.2.0
DoValue executes f with retrying policy. It is a shorthand of Policy.Start and Retrier.Continue. If f returns an error, retry to execute f until f returns nil error. If the error implements interface{ Temporary() bool } and Temporary() returns false, DoValue doesn't retry and returns the error.
Example ¶
policy := &retry.Policy{ MaxCount: 3, } count := 0 _, err := retry.DoValue(context.Background(), policy, func() (int, error) { count++ fmt.Printf("#%d: unstable func is called!\n", count) return 0, errors.New("some error!") }) if err != nil { fmt.Println(err) }
Output: #1: unstable func is called! #2: unstable func is called! #3: unstable func is called! some error!
func MarkPermanent ¶
MarkPermanent marks err as a permanent error. It returns the error that implements interface{ Temporary() bool } and Temporary() returns false.
Example ¶
policy := &retry.Policy{} err := policy.Do(context.Background(), func() error { fmt.Println("unstable func is called!") return retry.MarkPermanent(errors.New("some error!")) }) fmt.Println(err)
Output: unstable func is called! some error!
Types ¶
type Policy ¶
type Policy struct { // MinDelay is a first delay for retrying. // Zero or negative value means no delay. MinDelay time.Duration // MaxDelay is the maximum delay for retrying. // If MaxDelay is less than MinDelay, MinDelay is used as the maximum delay. MaxDelay time.Duration // MaxCount is max retry count. // Zero or negative value means retry forever. MaxCount int // Jitter adds random delay. // Zero means no jitter. // Negative value shorten the delay. Jitter time.Duration }
Policy is a retry policy.
func (*Policy) Do ¶
Do executes f with retrying policy. It is a shorthand of Policy.Start and Retrier.Continue. If f returns an error, retry to execute f until f returns nil error. If the error implements interface{ Temporary() bool } and Temporary() returns false, Do doesn't retry and returns the error.
Example ¶
policy := &retry.Policy{ MaxCount: 3, } count := 0 err := policy.Do(context.Background(), func() error { count++ fmt.Printf("#%d: unstable func is called!\n", count) return errors.New("some error!") }) if err != nil { fmt.Println(err) }
Output: #1: unstable func is called! #2: unstable func is called! #3: unstable func is called! some error!
func (*Policy) Start ¶
Start starts retrying
Example ¶
count := 0 unstableFunc := func() error { count++ fmt.Printf("#%d: unstableFunc is called!\n", count) if count < 3 { return errors.New("some error!") } return nil } policy := &retry.Policy{} retrier := policy.Start(context.Background()) for retrier.Continue() { err := unstableFunc() if err == nil { fmt.Println("Success") break } } if err := retrier.Err(); err != nil { log.Fatal(err) }
Output: #1: unstableFunc is called! #2: unstableFunc is called! #3: unstableFunc is called! Success