Skip to content

Commit

Permalink
Refactor: let 'time' module do the math, Fix: handle second variant o…
Browse files Browse the repository at this point in the history
…f 'retry-after' header

Signed-off-by: Jiri Vrba <jiri.vrba@firma.seznam.cz>
  • Loading branch information
Jiri Vrba committed Feb 26, 2024
1 parent ec0bcfc commit 55be54b
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/pkg/reg/adapter/dockerhub/adapter.go
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 55be54b

Please sign in to comment.