You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
408 B
26 lines
408 B
package model |
|
|
|
import ( |
|
"time" |
|
) |
|
|
|
// Retry is |
|
func Retry(attempts int, sleep time.Duration, fn func() error) error { |
|
if err := fn(); err != nil { |
|
if s, ok := err.(stop); ok { |
|
// Return the original error for later checking |
|
return s.error |
|
} |
|
|
|
if attempts--; attempts > 0 { |
|
time.Sleep(sleep) |
|
return Retry(attempts, 2*sleep, fn) |
|
} |
|
return err |
|
} |
|
return nil |
|
} |
|
|
|
type stop struct { |
|
error |
|
}
|
|
|