我正在启动两个并发执行路径(又名 goroutines)来执行两个作业
const Retries = 10
wg.Add(2)
go func() {
for i := 0; i < Retries; i++ {
retVal1, err1 = doSomeWork()
if err1 != nil {
fmt.Printf("WARNING: Error job in attempt no %d: %s - retrying...\n", i+1, err1)
continue
} else {
break
}
}
if err1 != nil {
log.Fatalf("ERROR job %s\n", err1)
}
wg.Done()
}()
go func() {
for i := 0; i < Retries; i++ {
retVal2, err2 = doSomeWork()
if err2 != nil {
fmt.Printf("WARNING: Error job in attempt no %d: %s - retrying...\n", i+1, err2)
continue
} else {
break
}
}
if err2 != nil {
log.Fatalf("ERROR job %s\n", err2)
}
wg.Done()
}()
wg.Wait()
我的问题是(除了导入一些第三方重试库)是否有更多惯用的方式来构建上述代码?渠道/选择方法会更加以去为中心吗?
人到中年有点甜
相关分类