1 回答
TA贡献1820条经验 获得超10个赞
我真的不明白为什么你的被处在一个单独的goroutine中。我认为每个工作进程都应该在执行任务之前退缩(如果需要的话)。我看到它是这样的:backOffProcess
backOffUntil := time.Now()
backOffMutex := sync.Mutex{}
go func() {
defer wg.Done()
for s := range jobC {
<-time.After(time.Until(backOffUntil))
limiter.Wait(ctx)
res, err := doSomeRequest(s)
if err != nil {
// Handle error.
log.Println(err)
}
// Handle rate limit.
if res.StatusCode == 429 {
delay, _ := strconv.Atoi(res.Header.Get("Retry-After"))
log.Println("rate limit hit, backing off")
// Back off.
newbackOffUntil := time.Now().Add(time.Second * delay)
backOffMutex.Lock()
if newbackOffUntil.Unix() > backOffUntil.Unix() {
backOffUntil = newbackOffUntil
}
backOffMutex.Unlock()
// Put serial back into job queue.
jobC <- s
}
resultC <- res
}
}()
- 1 回答
- 0 关注
- 98 浏览
添加回答
举报