@@ -8,53 +8,50 @@ package retry
88import (
99 "context"
1010 "fmt"
11-
12- "github.com/kamilsk/retry/v5/strategy"
1311)
1412
1513// Action defines a callable function that package retry can handle.
1614type Action = func (context.Context ) error
1715
18- // Error defines a string-based error without a different root cause.
19- type Error string
20-
21- // Error returns a string representation of an error.
22- func (err Error ) Error () string { return string (err ) }
23-
24- // Unwrap always returns nil means that an error doesn't have other root cause.
25- func (err Error ) Unwrap () error { return nil }
16+ // A Breaker carries a cancellation signal to interrupt an action execution.
17+ //
18+ // It is a subset of the built-in context and github.com/kamilsk/breaker interfaces.
19+ type Breaker = interface {
20+ // Done returns a channel that's closed when a cancellation signal occurred.
21+ Done () <- chan struct {}
22+ // If Done is not yet closed, Err returns nil.
23+ // If Done is closed, Err returns a non-nil error.
24+ // After Err returns a non-nil error, successive calls to Err return the same error.
25+ Err () error
26+ }
2627
2728// How is an alias for batch of Strategies.
2829//
2930// how := retry.How{
3031// strategy.Limit(3),
3132// }
3233//
33- type How = []func (strategy. Breaker , uint , error ) bool
34+ type How = []func (Breaker , uint , error ) bool
3435
3536// Do takes the action and performs it, repetitively, until successful.
3637//
3738// Optionally, strategies may be passed that assess whether or not an attempt
3839// should be made.
3940func Do (
40- breaker strategy. Breaker ,
41+ breaker Breaker ,
4142 action func (context.Context ) error ,
42- strategies ... func (strategy. Breaker , uint , error ) bool ,
43+ strategies ... func (Breaker , uint , error ) bool ,
4344) error {
4445 var (
45- err error = Error ("have no any try" )
46- clean error
46+ ctx = convert (breaker )
47+ err error = internal
48+ core error
4749 )
4850
49- ctx , is := breaker .(context.Context )
50- if ! is {
51- ctx = lite {context .Background (), breaker .Done ()}
52- }
53-
5451 for attempt , should := uint (0 ), true ; should ; attempt ++ {
55- clean = unwrap (err )
52+ core = unwrap (err )
5653 for i , repeat := 0 , len (strategies ); should && i < repeat ; i ++ {
57- should = should && strategies [i ](breaker , attempt , clean )
54+ should = should && strategies [i ](breaker , attempt , core )
5855 }
5956
6057 select {
@@ -78,9 +75,9 @@ func Do(
7875// Optionally, strategies may be passed that assess whether or not an attempt
7976// should be made.
8077func Go (
81- breaker strategy. Breaker ,
78+ breaker Breaker ,
8279 action func (context.Context ) error ,
83- strategies ... func (strategy. Breaker , uint , error ) bool ,
80+ strategies ... func (Breaker , uint , error ) bool ,
8481) error {
8582 done := make (chan error , 1 )
8683
0 commit comments