Skip to content

Commit

Permalink
Refactor: let 'time' module do the math
Browse files Browse the repository at this point in the history
Enhance/Fix: handle second variant of 'retry-after' header

Signed-off-by: Jiri Vrba <[email protected]>
  • Loading branch information
Jiri Vrba authored and Grimm75 committed Feb 26, 2024
1 parent b0faed0 commit bd59e2d
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/pkg/reg/adapter/dockerhub/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,27 @@ func (a *adapter) limitAwareDo(method string, path string, body io.Reader) (*htt
if reqsLeft < 8 {
resetTSC, err := strconv.ParseInt(clientResp.Header.Get("x-ratelimit-reset"), 10, 64)
if err == nil {
seconds := resetTSC - time.Now().Unix()
log.Infof("Ratelimit exhaustion eminent, sleeping for %d seconds", seconds)
time.Sleep(time.Duration(seconds) * time.Second)
dur := time.Until(time.Unix(resetTSC, 0))
log.Infof("Rate-limit exhaustion eminent, sleeping for %.1f seconds", dur.Seconds())
time.Sleep(dur)
log.Info("Sleep finished, resuming operation")
}
return clientResp, clientErr
}
return clientResp, clientErr
}
var dur = time.Duration(0)
seconds, err := strconv.ParseInt(clientResp.Header.Get("retry-after"), 10, 64)
if err != nil {
return nil, errors.New("blocked by dockerhub rate-limit and missing retry-after header")
expireTime, err := http.ParseTime(clientResp.Header.Get("retry-after"))
if err != nil {
return nil, errors.New("blocked by dockerhub rate-limit and missing retry-after header")
}
dur = time.Until(expireTime)
} else {
dur = time.Duration(seconds) * time.Second
}
log.Infof("Ratelimit exhausted, sleeping for %d seconds", seconds)
time.Sleep(time.Duration(seconds) * time.Second)
log.Infof("Rate-limit exhausted, sleeping for %.1f seconds", dur.Seconds())
time.Sleep(dur)
log.Info("Sleep finished, resuming operation")
attemptsLeft--
}
Expand Down

0 comments on commit bd59e2d

Please sign in to comment.