27 lines
408 B
Go
27 lines
408 B
Go
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
|
|
}
|