Skip to content

Commit

Permalink
add 重试规则 🐜
Browse files Browse the repository at this point in the history
  • Loading branch information
zanjs committed Jun 25, 2021
1 parent c7991ec commit 02e3176
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions retry/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ func RetryDurations(name string, max time.Duration, sleep time.Duration, callbac
}
}

// RetryTimes 重试规则
func RetryTimesRules(name string, rules []int, callback func() error) (err error) {
for i := 1; i <= len(rules); i++ {
err = callback()
if err == nil {
return nil
}
fmt.Printf("[%v]失败,第%v次重试, 错误信息:%s \n", name, i, err)
time.Sleep(time.Duration(rules[i] * time.Now().Second()))
}
err = fmt.Errorf("[%v]失败,共重试%d次, 最近一次错误:%s \n", name, len(rules), err)
fmt.Println(err)
return err

}

0 comments on commit 02e3176

Please sign in to comment.