1234567891011121314151617181920212223242526 |
- 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
- }
|