go-common/app/job/main/relation-cache/model/retry.go
2019-04-22 18:49:16 +08:00

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
}