@@ -117,72 +117,61 @@ func Do(retryableFunc RetryableFunc, opts ...Option) error {
117117 return nil
118118 }
119119
120- var errorLog Error
121- if ! config .lastErrorOnly {
122- errorLog = make (Error , config .attempts )
123- } else {
124- errorLog = make (Error , 1 )
125- }
120+ errorLog := Error {}
126121
127122 attemptsForError := make (map [error ]uint , len (config .attemptsForError ))
128123 for err , attempts := range config .attemptsForError {
129124 attemptsForError [err ] = attempts
130125 }
131126
132- lastErrIndex := n
133127 shouldRetry := true
134128 for shouldRetry {
135129 err := retryableFunc ()
130+ if err == nil {
131+ return nil
132+ }
136133
137- if err != nil {
138- errorLog [lastErrIndex ] = unpackUnrecoverable (err )
134+ errorLog = append (errorLog , unpackUnrecoverable (err ))
139135
140- if ! config .retryIf (err ) {
141- break
142- }
136+ if ! config .retryIf (err ) {
137+ break
138+ }
143139
144- config .onRetry (n , err )
140+ config .onRetry (n , err )
145141
146- for errToCheck , attempts := range attemptsForError {
147- if errors .Is (err , errToCheck ) {
148- attempts --
149- attemptsForError [errToCheck ] = attempts
150- shouldRetry = shouldRetry && attempts > 0
151- }
142+ for errToCheck , attempts := range attemptsForError {
143+ if errors .Is (err , errToCheck ) {
144+ attempts --
145+ attemptsForError [errToCheck ] = attempts
146+ shouldRetry = shouldRetry && attempts > 0
152147 }
148+ }
153149
154- // if this is last attempt - don't wait
155- if n == config .attempts - 1 {
156- break
157- }
150+ // if this is last attempt - don't wait
151+ if n == config .attempts - 1 {
152+ break
153+ }
158154
159- select {
160- case <- config .timer .After (delay (config , n , err )):
161- case <- config .context .Done ():
162- if config .lastErrorOnly {
163- return config .context .Err ()
164- }
165- n ++
166- errorLog [n ] = config .context .Err ()
167- return errorLog [:lenWithoutNil (errorLog )]
155+ select {
156+ case <- config .timer .After (delay (config , n , err )):
157+ case <- config .context .Done ():
158+ if config .lastErrorOnly {
159+ return config .context .Err ()
168160 }
161+ n ++
169162
170- } else {
171- return nil
163+ return append (errorLog , config .context .Err ())
172164 }
173165
174166 n ++
175167 shouldRetry = shouldRetry && n < config .attempts
176-
177- if ! config .lastErrorOnly {
178- lastErrIndex = n
179- }
180168 }
181169
182170 if config .lastErrorOnly {
183- return errorLog [ lastErrIndex ]
171+ return errorLog . Unwrap ()
184172 }
185- return errorLog [:lenWithoutNil (errorLog )]
173+
174+ return errorLog
186175}
187176
188177func newDefaultRetryConfig () * Config {
@@ -206,7 +195,7 @@ type Error []error
206195// Error method return string representation of Error
207196// It is an implementation of error interface
208197func (e Error ) Error () string {
209- logWithNumber := make ([]string , lenWithoutNil (e ))
198+ logWithNumber := make ([]string , len (e ))
210199 for i , l := range e {
211200 if l != nil {
212201 logWithNumber [i ] = fmt .Sprintf ("#%d: %s" , i + 1 , l .Error ())
@@ -250,17 +239,7 @@ When you need to unwrap all errors, you should use `WrappedErrors()` instead.
250239Added in version 4.2.0.
251240*/
252241func (e Error ) Unwrap () error {
253- return e [lenWithoutNil (e )- 1 ]
254- }
255-
256- func lenWithoutNil (e Error ) (count int ) {
257- for _ , v := range e {
258- if v != nil {
259- count ++
260- }
261- }
262-
263- return
242+ return e [len (e )- 1 ]
264243}
265244
266245// WrappedErrors returns the list of errors that this Error is wrapping.
0 commit comments