Skip to content

Commit

Permalink
Fix: review - clarify return values
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Vrba <jiri.vrba@firma.seznam.cz>
  • Loading branch information
Jiri Vrba authored and Grimm75 committed Feb 26, 2024
1 parent dcb57d4 commit b0faed0
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions src/pkg/reg/adapter/dockerhub/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,35 +135,33 @@ func getAdapterInfo() *model.AdapterPattern {
func (a *adapter) limitAwareDo(method string, path string, body io.Reader) (*http.Response, error) {
var attemptsLeft = 3
for attemptsLeft > 0 {
resp, err := a.client.Do(method, path, body)
if err != nil {
return resp, err
clientResp, clientErr := a.client.Do(method, path, body)
if clientErr != nil {
return clientResp, clientErr
}
if resp.StatusCode == http.StatusTooManyRequests {
seconds, err := strconv.ParseInt(resp.Header.Get("retry-after"), 10, 64)
if clientResp.StatusCode != http.StatusTooManyRequests {
reqsLeft, err := strconv.ParseInt(clientResp.Header.Get("x-ratelimit-remaining"), 10, 64)
if err != nil {
return nil, errors.New("blocked by dockerhub rate-limit and missing retry-after header")
}
log.Infof("Ratelimit exhausted, sleeping for %d seconds", seconds)
time.Sleep(time.Duration(seconds) * time.Second)
log.Info("Sleep finished, resuming operation")
} else {
reqsLeft, convErr := strconv.ParseInt(resp.Header.Get("x-ratelimit-remaining"), 10, 64)
if convErr != nil {
return resp, err
return clientResp, clientErr
}
if reqsLeft < 8 {
resetTSC, convErr := strconv.ParseInt(resp.Header.Get("x-ratelimit-reset"), 10, 64)
if convErr != nil {
return resp, err
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)
log.Info("Sleep finished, resuming operation")
}
seconds := resetTSC - time.Now().Unix()
log.Infof("Ratelimit exhaustion eminent, sleeping for %d seconds", seconds)
time.Sleep(time.Duration(seconds) * time.Second)
log.Info("Sleep finished, resuming operation")
}
return resp, err
return clientResp, clientErr
}
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")
}
log.Infof("Ratelimit exhausted, sleeping for %d seconds", seconds)
time.Sleep(time.Duration(seconds) * time.Second)
log.Info("Sleep finished, resuming operation")
attemptsLeft--
}
return nil, errors.New("unable to get past dockerhub rate-limit")
Expand Down

0 comments on commit b0faed0

Please sign in to comment.