-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docker: handle http 429 status codes
Consolidate checking the http-status codes to allow for a more uniform error handling. Also treat code 429 (too many requests) as a known error instead of an invalid status code. When hitting 429, perform an exponential back off starting a 2 seconds for at most 5 iterations. If the http.Response set the `Retry-Header` then use the provided value or date to compute the delay until the next attempt. Note that the maximum delay is 60 seconds. Signed-off-by: Valentin Rothberg <[email protected]>
- Loading branch information
Showing
5 changed files
with
118 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package docker | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
var ( | ||
// ErrV1NotSupported is returned when we're trying to talk to a | ||
// docker V1 registry. | ||
ErrV1NotSupported = errors.New("can't talk to a V1 docker registry") | ||
// ErrUnauthorizedForCredentials is returned when the status code returned is 401 | ||
ErrUnauthorizedForCredentials = errors.New("unable to retrieve auth token: invalid username/password") | ||
// ErrTooManyRequests is returned when the status code returned is 429 | ||
ErrTooManyRequests = errors.New("too many request to registry") | ||
) | ||
|
||
// httpResponseToError translates the https.Response into an error. It returns | ||
// nil if the response is not considered an error. | ||
func httpResponseToError(res *http.Response) error { | ||
switch res.StatusCode { | ||
case http.StatusOK: | ||
return nil | ||
case http.StatusTooManyRequests: | ||
return ErrTooManyRequests | ||
case http.StatusUnauthorized: | ||
return ErrUnauthorizedForCredentials | ||
default: | ||
return errors.Errorf("invalid status code from registry %d (%s)", res.StatusCode, http.StatusText(res.StatusCode)) | ||
} | ||
} |