【发布时间】:2021-11-29 21:38:46
【问题描述】:
刚刚开始了解 goroutine 的强大功能。
我有约 100 个帐户和约 10 个区域,循环它们以使用 golang 创建约 1000 个 goroutine 以提高阅读速度。它运行得太快,以至于达到了 20/秒的 API 返回限制。
如何确保所有的 goroutine 都能维持在最大调用率(20/s)?我不确定哪种 golang 并发方法最适合一起处理错误。
例如:
func readInstance(acc string, region string, wg *sync.WaitGroup) {
defer wg.Done()
response, err := client.DescribeInstances(acc, region)
if err != nil {
log.Println(err) // API limit exceeding 20
return
}
.
.
.
.
}
func main() {
accounts := []string{"g", "h", "i", ...}
regions := []string{"g", "h", "i", ...}
for _, region := range regions {
for i := 0; i < len(accounts); i++ {
wg.Add(1)
go readInstance(accounts[i], region, &wg)
}
}
wg.Wait()
}
【问题讨论】: