-
Notifications
You must be signed in to change notification settings - Fork 120
Add retry logic for retrieving top layer SHA with registry mirrors #1675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fe63855
Add retry logic for retrieving top layer SHA with registry mirrors
vpelikh ecdc091
Add retry logic for registry mirror top layer SHA retrieval
vpelikh cd6c362
Add back error handling for top layer SHA retrieval in rebaser
vpelikh 3e6e33f
Add comments for ExportOptions and Rebaser types
vpelikh d31c075
Replace factory pattern with OpenRemoteImage retry
vpelikh b24e991
Revert formatting changes
vpelikh cab1bd7
Extract retry logic to dedicated retry.go file
vpelikh 74a1869
Add tests for isRetryable and OpenRemoteImage
vpelikh 867db97
Fix unused-parameter lint error in testIsRetryable
vpelikh 15038e3
Formatting
vpelikh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,65 @@ | ||
| package phase | ||
|
|
||
| import ( | ||
| stderrors "errors" | ||
| "net/http" | ||
| "time" | ||
|
|
||
| "github.com/buildpacks/imgutil" | ||
| "github.com/google/go-containerregistry/pkg/v1/remote/transport" | ||
|
|
||
| "github.com/buildpacks/lifecycle/log" | ||
| ) | ||
|
|
||
| // topLayerDelays is hardcoded array of delays | ||
| var topLayerDelays = []time.Duration{ | ||
| 100 * time.Millisecond, | ||
| 200 * time.Millisecond, | ||
| 500 * time.Millisecond, | ||
| 1 * time.Second, | ||
| 2 * time.Second, | ||
| } | ||
|
|
||
| // topLayerSleep is the function used for sleeping between retries. | ||
| // It can be replaced for testing. | ||
| var topLayerSleep = time.Sleep | ||
|
|
||
| // isRetryable returns true if the error is likely transient and should be retried. | ||
| // 401 Unauthorized and 403 Forbidden are not retryable as they indicate auth/config issues. | ||
| func isRetryable(err error) bool { | ||
| if tErr, ok := stderrors.AsType[*transport.Error](err); ok { | ||
| return tErr.StatusCode != http.StatusBadRequest && | ||
| tErr.StatusCode != http.StatusUnauthorized && | ||
| tErr.StatusCode != http.StatusForbidden && | ||
| tErr.StatusCode != http.StatusMethodNotAllowed && | ||
| tErr.StatusCode != http.StatusTooManyRequests | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| // OpenRemoteImage opens a remote image with retry logic for registry mirror transient errors. | ||
| // go-containerregistry caches manifests, so each retry attempt creates a fresh image. | ||
| // Non-retryable errors (401, 403) are returned immediately without retry. | ||
| func OpenRemoteImage(logger log.Logger, newImage func() (imgutil.Image, error)) (imgutil.Image, error) { | ||
| var lastErr error | ||
| for attempt := 0; attempt <= len(topLayerDelays); attempt++ { | ||
| img, err := newImage() | ||
| if err == nil { | ||
| if _, err = img.TopLayer(); err == nil { | ||
| if attempt > 0 { | ||
| logger.Infof("Successfully opened remote image after %d retries", attempt) | ||
| } | ||
| return img, nil | ||
| } | ||
| } | ||
| lastErr = err | ||
| if !isRetryable(err) { | ||
| return nil, err | ||
| } | ||
| if attempt < len(topLayerDelays) { | ||
| logger.Warnf("Failed to open remote image (attempt %d/%d): %v, retrying in %v", attempt+1, len(topLayerDelays)+1, err, topLayerDelays[attempt]) | ||
| topLayerSleep(topLayerDelays[attempt]) | ||
| } | ||
| } | ||
| return nil, lastErr | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jabrown85, linting fail without comment for ExportOptions and Rebaser. Should I keep it?