1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #6727 from unclejack/improve_bytes_range

Improve layer downloading
This commit is contained in:
Vincent Batts 2014-07-23 15:31:25 -04:00
commit 683038bf57
2 changed files with 27 additions and 33 deletions

View file

@ -390,52 +390,42 @@ func (r *Registry) GetRemoteImageJSON(imgID, registry string, token []string) ([
func (r *Registry) GetRemoteImageLayer(imgID, registry string, token []string, imgSize int64) (io.ReadCloser, error) { func (r *Registry) GetRemoteImageLayer(imgID, registry string, token []string, imgSize int64) (io.ReadCloser, error) {
var ( var (
retries = 5 retries = 5
headRes *http.Response client *http.Client
client *http.Client res *http.Response
hasResume bool = false imageURL = fmt.Sprintf("%simages/%s/layer", registry, imgID)
imageURL = fmt.Sprintf("%simages/%s/layer", registry, imgID)
) )
headReq, err := r.reqFactory.NewRequest("HEAD", imageURL, nil)
if err != nil {
return nil, fmt.Errorf("Error while getting from the server: %s\n", err)
}
setTokenAuth(headReq, token)
for i := 1; i <= retries; i++ {
headRes, client, err = r.doRequest(headReq)
if err != nil && i == retries {
return nil, fmt.Errorf("Eror while making head request: %s\n", err)
} else if err != nil {
time.Sleep(time.Duration(i) * 5 * time.Second)
continue
}
break
}
if headRes.Header.Get("Accept-Ranges") == "bytes" && imgSize > 0 {
hasResume = true
}
req, err := r.reqFactory.NewRequest("GET", imageURL, nil) req, err := r.reqFactory.NewRequest("GET", imageURL, nil)
if err != nil { if err != nil {
return nil, fmt.Errorf("Error while getting from the server: %s\n", err) return nil, fmt.Errorf("Error while getting from the server: %s\n", err)
} }
setTokenAuth(req, token) setTokenAuth(req, token)
if hasResume { for i := 1; i <= retries; i++ {
utils.Debugf("server supports resume") res, client, err = r.doRequest(req)
return utils.ResumableRequestReader(client, req, 5, imgSize), nil if err != nil {
} res.Body.Close()
utils.Debugf("server doesn't support resume") if i == retries {
res, _, err := r.doRequest(req) return nil, fmt.Errorf("Server error: Status %d while fetching image layer (%s)",
if err != nil { res.StatusCode, imgID)
return nil, err }
time.Sleep(time.Duration(i) * 5 * time.Second)
continue
}
break
} }
if res.StatusCode != 200 { if res.StatusCode != 200 {
res.Body.Close() res.Body.Close()
return nil, fmt.Errorf("Server error: Status %d while fetching image layer (%s)", return nil, fmt.Errorf("Server error: Status %d while fetching image layer (%s)",
res.StatusCode, imgID) res.StatusCode, imgID)
} }
if res.Header.Get("Accept-Ranges") == "bytes" && imgSize > 0 {
utils.Debugf("server supports resume")
return utils.ResumableRequestReaderWithInitialResponse(client, req, 5, imgSize, res), nil
}
utils.Debugf("server doesn't support resume")
return res.Body, nil return res.Body, nil
} }

View file

@ -24,6 +24,10 @@ func ResumableRequestReader(c *http.Client, r *http.Request, maxfail uint32, tot
return &resumableRequestReader{client: c, request: r, maxFailures: maxfail, totalSize: totalsize} return &resumableRequestReader{client: c, request: r, maxFailures: maxfail, totalSize: totalsize}
} }
func ResumableRequestReaderWithInitialResponse(c *http.Client, r *http.Request, maxfail uint32, totalsize int64, initialResponse *http.Response) io.ReadCloser {
return &resumableRequestReader{client: c, request: r, maxFailures: maxfail, totalSize: totalsize, currentResponse: initialResponse}
}
func (r *resumableRequestReader) Read(p []byte) (n int, err error) { func (r *resumableRequestReader) Read(p []byte) (n int, err error) {
if r.client == nil || r.request == nil { if r.client == nil || r.request == nil {
return 0, fmt.Errorf("client and request can't be nil\n") return 0, fmt.Errorf("client and request can't be nil\n")