package main标签:index,fmt,重试,Println,bool,func,go,机制 From: https://www.cnblogs.com/paulversion/p/16599950.html
import (
"fmt"
"time"
)
var RetryFlag chan bool = make(chan bool)
func main() {
Retry(test,[]int{3,5,10})
}
//重试次数
func Retry(f func()bool,rules[]int) {
fmt.Println("========>>进入重试机制======>")
index := 0
for {
go time.AfterFunc(time.Duration(rules[index])*time.Second, func() {
fmt.Println("========>执行重试方法=========>")
RetryFlag <-f()
})
if <- RetryFlag {
fmt.Println("======>重试成功=======>")
return
}
if index == len(rules) - 1 {
fmt.Println("======>达到最大重试次数=====>")
return
}
index ++
}
}
func test() bool {
return false
}