-
Notifications
You must be signed in to change notification settings - Fork 601
feat(transport): add resumable transport for remote resources #2152
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
Open
derekhjray
wants to merge
4
commits into
google:main
Choose a base branch
from
derekhjray:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+723
−0
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
619c7ca
feat(transport): add resumable transport for remote resources
derekhjray 2600811
feat(transport): using loops instead of labels for resumableBoyd.Read()
derekhjray 6e297fb
optimize(transport): optimize resumable transport implementaion
derekhjray 17f518a
feat(transport): add resumable backoff option support
derekhjray 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,242 @@ | ||
| package transport | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "regexp" | ||
| "strconv" | ||
| "strings" | ||
| "sync/atomic" | ||
|
|
||
| "github.com/google/go-containerregistry/pkg/logs" | ||
| ) | ||
|
|
||
| // NewResumable creates a http.RoundTripper that resumes http GET from error, | ||
| // and the inner should be wrapped with retry transport, otherwise, the | ||
| // transport will abort if resume() returns error. | ||
| func NewResumable(inner http.RoundTripper) http.RoundTripper { | ||
| return &resumableTransport{inner: inner} | ||
| } | ||
|
|
||
| var ( | ||
| contentRangeRe = regexp.MustCompile(`^bytes (\d+)-(\d+)/(\d+|\*)$`) | ||
| ) | ||
|
|
||
| type resumableTransport struct { | ||
| inner http.RoundTripper | ||
| } | ||
|
|
||
| func (rt *resumableTransport) RoundTrip(in *http.Request) (*http.Response, error) { | ||
| if in.Method != http.MethodGet { | ||
| return rt.inner.RoundTrip(in) | ||
| } | ||
|
|
||
| req := in.Clone(in.Context()) | ||
| req.Header.Set("Range", "bytes=0-") | ||
| resp, err := rt.inner.RoundTrip(req) | ||
| if err != nil { | ||
| return resp, err | ||
| } | ||
|
|
||
| switch resp.StatusCode { | ||
| case http.StatusPartialContent: | ||
| case http.StatusRequestedRangeNotSatisfiable: | ||
| // fallback to previous behavior | ||
| resp.Body.Close() | ||
| return rt.inner.RoundTrip(in) | ||
| default: | ||
| return resp, nil | ||
| } | ||
|
|
||
| var contentLength int64 | ||
| if _, _, contentLength, err = parseContentRange(resp.Header.Get("Content-Range")); err != nil || contentLength <= 0 { | ||
| // fallback to previous behavior | ||
| resp.Body.Close() | ||
| return rt.inner.RoundTrip(in) | ||
| } | ||
|
|
||
| // modify response status to 200, ensure caller error checking works | ||
| resp.StatusCode = http.StatusOK | ||
| resp.Status = "200 OK" | ||
| resp.ContentLength = contentLength | ||
| resp.Body = &resumableBody{ | ||
| rc: resp.Body, | ||
| inner: rt.inner, | ||
| req: req, | ||
| total: contentLength, | ||
| transferred: 0, | ||
| } | ||
|
|
||
| return resp, nil | ||
| } | ||
|
|
||
| type resumableBody struct { | ||
| rc io.ReadCloser | ||
|
|
||
| inner http.RoundTripper | ||
| req *http.Request | ||
|
|
||
| transferred int64 | ||
| total int64 | ||
|
|
||
| closed uint32 | ||
| } | ||
|
|
||
| func (rb *resumableBody) Read(p []byte) (n int, err error) { | ||
| if atomic.LoadUint32(&rb.closed) == 1 { | ||
| // response body already closed | ||
| return 0, http.ErrBodyReadAfterClose | ||
| } else if rb.total >= 0 && rb.transferred >= rb.total { | ||
| return 0, io.EOF | ||
| } | ||
|
|
||
| resume: | ||
| if n, err = rb.rc.Read(p); n > 0 { | ||
| rb.transferred += int64(n) | ||
| } | ||
|
|
||
| if err == nil { | ||
| return | ||
| } | ||
|
|
||
| if errors.Is(err, io.EOF) && rb.total >= 0 && rb.transferred == rb.total { | ||
| return | ||
| } | ||
|
|
||
| if err = rb.resume(err); err == nil { | ||
| if n == 0 { | ||
| // zero bytes read, try reading again with new response.Body | ||
| goto resume | ||
| } | ||
|
|
||
| // already read some bytes from previous response.Body, returns and waits for next Read operation | ||
| } | ||
|
|
||
| return n, err | ||
| } | ||
|
|
||
| func (rb *resumableBody) Close() (err error) { | ||
| if !atomic.CompareAndSwapUint32(&rb.closed, 0, 1) { | ||
| return nil | ||
| } | ||
|
|
||
| return rb.rc.Close() | ||
| } | ||
|
|
||
| func (rb *resumableBody) resume(reason error) error { | ||
| if reason != nil { | ||
| logs.Debug.Printf("Resume http transporting from error: %v", reason) | ||
| } | ||
|
|
||
| ctx := rb.req.Context() | ||
| select { | ||
| case <-ctx.Done(): | ||
| // context already done, stop resuming from error | ||
| return ctx.Err() | ||
| default: | ||
| } | ||
|
|
||
| req := rb.req.Clone(ctx) | ||
| req.Header.Set("Range", "bytes="+strconv.FormatInt(rb.transferred, 10)+"-") | ||
| resp, err := rb.inner.RoundTrip(req) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err = rb.validate(resp); err != nil { | ||
| resp.Body.Close() | ||
| return err | ||
| } | ||
|
|
||
| if atomic.LoadUint32(&rb.closed) == 1 { | ||
| resp.Body.Close() | ||
| return http.ErrBodyReadAfterClose | ||
| } | ||
|
|
||
| rb.rc.Close() | ||
| rb.rc = resp.Body | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (rb *resumableBody) validate(resp *http.Response) (err error) { | ||
| var start, total int64 | ||
| switch resp.StatusCode { | ||
| case http.StatusPartialContent: | ||
| if start, _, total, err = parseContentRange(resp.Header.Get("Content-Range")); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if total > rb.total { | ||
| rb.total = total | ||
| } | ||
|
|
||
| if start == rb.transferred { | ||
| break | ||
| } else if start < rb.transferred { | ||
| if _, err := io.CopyN(io.Discard, resp.Body, rb.transferred-start); err != nil { | ||
| return fmt.Errorf("discard overlapped data failed, %v", err) | ||
| } | ||
| } else { | ||
| return fmt.Errorf("unexpected resume start %d, wanted: %d", start, rb.transferred) | ||
| } | ||
| case http.StatusOK: | ||
| if rb.transferred > 0 { | ||
| if _, err = io.CopyN(io.Discard, resp.Body, rb.transferred); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| case http.StatusRequestedRangeNotSatisfiable: | ||
| if contentRange := resp.Header.Get("Content-Range"); contentRange != "" && strings.HasPrefix(contentRange, "bytes */") { | ||
| if total, err = strconv.ParseInt(strings.TrimPrefix(contentRange, "bytes */"), 10, 64); err == nil && total >= 0 && rb.transferred >= total { | ||
| return io.EOF | ||
| } | ||
| } | ||
|
|
||
| fallthrough | ||
| default: | ||
| return fmt.Errorf("unexpected status code %d", resp.StatusCode) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func parseContentRange(contentRange string) (start, end, size int64, err error) { | ||
| if contentRange == "" { | ||
| return -1, -1, -1, errors.New("unexpected empty content range") | ||
| } | ||
|
|
||
| matches := contentRangeRe.FindStringSubmatch(contentRange) | ||
| if len(matches) != 4 { | ||
| return -1, -1, -1, fmt.Errorf("invalid content range: %s", contentRange) | ||
| } | ||
|
|
||
| if start, err = strconv.ParseInt(matches[1], 10, 64); err != nil { | ||
| return -1, -1, -1, fmt.Errorf("unexpected start from content range '%s', %v", contentRange, err) | ||
| } | ||
|
|
||
| if end, err = strconv.ParseInt(matches[2], 10, 64); err != nil { | ||
| return -1, -1, -1, fmt.Errorf("unexpected end from content range '%s', %v", contentRange, err) | ||
| } | ||
|
|
||
| if start > end { | ||
| return -1, -1, -1, fmt.Errorf("invalid content range: %s", contentRange) | ||
| } | ||
|
|
||
| if matches[3] == "*" { | ||
| size = -1 | ||
| } else { | ||
| size, err = strconv.ParseInt(matches[3], 10, 64) | ||
| if err != nil { | ||
| return -1, -1, -1, fmt.Errorf("unexpected total from content range '%s', %v", contentRange, err) | ||
| } | ||
|
|
||
| if end >= size { | ||
| return -1, -1, -1, fmt.Errorf("invalid content range: %s", contentRange) | ||
| } | ||
| } | ||
|
|
||
| return | ||
| } | ||
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.
Please avoid labels and express this as a loop.
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.
My bad, I'm not familiar with Github's systems. I'll raise this in a review.
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.
(reopening)
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.
modified