@@ -117,8 +117,14 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
117117 errorLog = make (Error , 1 )
118118 }
119119
120+ attemptsForError := make (map [error ]uint , len (config .attemptsForError ))
121+ for err , attempts := range config .attemptsForError {
122+ attemptsForError [err ] = attempts
123+ }
124+
120125 lastErrIndex := n
121- for n < config .attempts {
126+ shouldRetry := true
127+ for shouldRetry {
122128 err := retryableFunc ()
123129
124130 if err != nil {
@@ -130,6 +136,14 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
130136
131137 config .onRetry (n , err )
132138
139+ for errToCheck , attempts := range attemptsForError {
140+ if errors .Is (err , errToCheck ) {
141+ attempts --
142+ attemptsForError [errToCheck ] = attempts
143+ shouldRetry = shouldRetry && attempts > 0
144+ }
145+ }
146+
133147 // if this is last attempt - don't wait
134148 if n == config .attempts - 1 {
135149 break
@@ -150,6 +164,8 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
150164 }
151165
152166 n ++
167+ shouldRetry = shouldRetry && n < config .attempts
168+
153169 if ! config .lastErrorOnly {
154170 lastErrIndex = n
155171 }
@@ -163,15 +179,16 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
163179
164180func newDefaultRetryConfig () * Config {
165181 return & Config {
166- attempts : uint (10 ),
167- delay : 100 * time .Millisecond ,
168- maxJitter : 100 * time .Millisecond ,
169- onRetry : func (n uint , err error ) {},
170- retryIf : IsRecoverable ,
171- delayType : CombineDelay (BackOffDelay , RandomDelay ),
172- lastErrorOnly : false ,
173- context : context .Background (),
174- timer : & timerImpl {},
182+ attempts : uint (10 ),
183+ attemptsForError : make (map [error ]uint ),
184+ delay : 100 * time .Millisecond ,
185+ maxJitter : 100 * time .Millisecond ,
186+ onRetry : func (n uint , err error ) {},
187+ retryIf : IsRecoverable ,
188+ delayType : CombineDelay (BackOffDelay , RandomDelay ),
189+ lastErrorOnly : false ,
190+ context : context .Background (),
191+ timer : & timerImpl {},
175192 }
176193}
177194
0 commit comments