Merge pull request #547 from ywk253100/connection_close

Close connection immediately when it is not used anymore
This commit is contained in:
Daniel Jiang 2016-07-14 21:05:46 +08:00 committed by GitHub
commit 2558ab0146
7 changed files with 31 additions and 26 deletions

View File

@ -181,13 +181,14 @@ func getRepoList(projectID int64) ([]string, error) {
log.Errorf("Error when calling UI api to get repositories, error: %v", err) log.Errorf("Error when calling UI api to get repositories, error: %v", err)
return nil, err return nil, err
} }
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
log.Errorf("Unexpected status code: %d", resp.StatusCode) log.Errorf("Unexpected status code: %d", resp.StatusCode)
dump, _ := httputil.DumpResponse(resp, true) dump, _ := httputil.DumpResponse(resp, true)
log.Debugf("response: %q", dump) log.Debugf("response: %q", dump)
return nil, fmt.Errorf("Unexpected status code when getting repository list: %d", resp.StatusCode) return nil, fmt.Errorf("Unexpected status code when getting repository list: %d", resp.StatusCode)
} }
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Errorf("Failed to read the response body, error: %v", err) log.Errorf("Failed to read the response body, error: %v", err)

View File

@ -155,7 +155,7 @@ func (ra *RepJobAPI) GetLog() {
log.Errorf("failed to get log for job %d: %v", ra.jobID, err) log.Errorf("failed to get log for job %d: %v", ra.jobID, err)
ra.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) ra.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
ra.Ctx.ResponseWriter.Header().Set(http.CanonicalHeaderKey("Content-Length"), resp.Header.Get(http.CanonicalHeaderKey("Content-Length"))) ra.Ctx.ResponseWriter.Header().Set(http.CanonicalHeaderKey("Content-Length"), resp.Header.Get(http.CanonicalHeaderKey("Content-Length")))
ra.Ctx.ResponseWriter.Header().Set(http.CanonicalHeaderKey("Content-Type"), "text/plain") ra.Ctx.ResponseWriter.Header().Set(http.CanonicalHeaderKey("Content-Type"), "text/plain")
@ -167,7 +167,6 @@ func (ra *RepJobAPI) GetLog() {
return return
} }
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
log.Errorf("failed to read reponse body: %v", err) log.Errorf("failed to read reponse body: %v", err)

View File

@ -119,13 +119,12 @@ func TriggerReplication(policyID int64, repository string,
if err != nil { if err != nil {
return err return err
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
return nil return nil
} }
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body) b, err = ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
@ -194,12 +193,12 @@ func postReplicationAction(policyID int64, acton string) error {
return err return err
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
return nil return nil
} }
defer resp.Body.Close()
b, err = ioutil.ReadAll(resp.Body) b, err = ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err

View File

@ -250,6 +250,8 @@ func (c *Checker) projectExist() (exist, canWrite bool, err error) {
return return
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound { if resp.StatusCode == http.StatusNotFound {
return return
} }
@ -259,7 +261,6 @@ func (c *Checker) projectExist() (exist, canWrite bool, err error) {
return return
} }
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body) data, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return return
@ -328,6 +329,8 @@ func (c *Checker) createProject(isPublic bool) error {
return err return err
} }
defer resp.Body.Close()
// version 0.1.1's reponse code is 200 // version 0.1.1's reponse code is 200
if resp.StatusCode == http.StatusCreated || if resp.StatusCode == http.StatusCreated ||
resp.StatusCode == http.StatusOK { resp.StatusCode == http.StatusOK {
@ -338,7 +341,6 @@ func (c *Checker) createProject(isPublic bool) error {
return ErrConflict return ErrConflict
} }
defer resp.Body.Close()
message, err := ioutil.ReadAll(resp.Body) message, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
c.logger.Errorf("an error occurred while reading message from response: %v", err) c.logger.Errorf("an error occurred while reading message from response: %v", err)
@ -460,6 +462,9 @@ func (b *BlobTransfer) enter() (string, error) {
b.logger.Errorf("an error occurred while pulling blob %s of %s:%s from %s: %v", blob, name, tag, b.srcURL, err) b.logger.Errorf("an error occurred while pulling blob %s of %s:%s from %s: %v", blob, name, tag, b.srcURL, err)
return "", err return "", err
} }
if data != nil {
defer data.Close()
}
if err = b.dstClient.PushBlob(blob, size, data); err != nil { if err = b.dstClient.PushBlob(blob, size, data); err != nil {
b.logger.Errorf("an error occurred while pushing blob %s of %s:%s to %s : %v", blob, name, tag, b.dstURL, err) b.logger.Errorf("an error occurred while pushing blob %s of %s:%s to %s : %v", blob, name, tag, b.dstURL, err)
return "", err return "", err

View File

@ -55,6 +55,7 @@ func NewAuthorizerStore(endpoint string, insecure bool, authorizers ...Authorize
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer resp.Body.Close()
challenges := ParseChallengeFromResponse(resp) challenges := ParseChallengeFromResponse(resp)
return &AuthorizerStore{ return &AuthorizerStore{

View File

@ -131,13 +131,12 @@ func (r *Registry) Ping() error {
if err != nil { if err != nil {
return parseError(err) return parseError(err)
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
return nil return nil
} }
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err

View File

@ -150,6 +150,8 @@ func (r *Repository) ManifestExist(reference string) (digest string, exist bool,
return return
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
exist = true exist = true
digest = resp.Header.Get(http.CanonicalHeaderKey("Docker-Content-Digest")) digest = resp.Header.Get(http.CanonicalHeaderKey("Docker-Content-Digest"))
@ -160,8 +162,6 @@ func (r *Repository) ManifestExist(reference string) (digest string, exist bool,
return return
} }
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return return
@ -227,13 +227,13 @@ func (r *Repository) PushManifest(reference, mediaType string, payload []byte) (
return return
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusCreated { if resp.StatusCode == http.StatusCreated {
digest = resp.Header.Get(http.CanonicalHeaderKey("Docker-Content-Digest")) digest = resp.Header.Get(http.CanonicalHeaderKey("Docker-Content-Digest"))
return return
} }
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return return
@ -259,12 +259,12 @@ func (r *Repository) DeleteManifest(digest string) error {
return parseError(err) return parseError(err)
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusAccepted { if resp.StatusCode == http.StatusAccepted {
return nil return nil
} }
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
@ -304,6 +304,8 @@ func (r *Repository) BlobExist(digest string) (bool, error) {
return false, parseError(err) return false, parseError(err)
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK { if resp.StatusCode == http.StatusOK {
return true, nil return true, nil
} }
@ -312,8 +314,6 @@ func (r *Repository) BlobExist(digest string) (bool, error) {
return false, nil return false, nil
} }
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return false, err return false, err
@ -325,7 +325,7 @@ func (r *Repository) BlobExist(digest string) (bool, error) {
} }
} }
// PullBlob ... // PullBlob : client must close data if it is not nil
func (r *Repository) PullBlob(digest string) (size int64, data io.ReadCloser, err error) { func (r *Repository) PullBlob(digest string) (size int64, data io.ReadCloser, err error) {
req, err := http.NewRequest("GET", buildBlobURL(r.Endpoint.String(), r.Name, digest), nil) req, err := http.NewRequest("GET", buildBlobURL(r.Endpoint.String(), r.Name, digest), nil)
if err != nil { if err != nil {
@ -349,6 +349,7 @@ func (r *Repository) PullBlob(digest string) (size int64, data io.ReadCloser, er
} }
defer resp.Body.Close() defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return return
@ -372,14 +373,14 @@ func (r *Repository) initiateBlobUpload(name string) (location, uploadUUID strin
return return
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusAccepted { if resp.StatusCode == http.StatusAccepted {
location = resp.Header.Get(http.CanonicalHeaderKey("Location")) location = resp.Header.Get(http.CanonicalHeaderKey("Location"))
uploadUUID = resp.Header.Get(http.CanonicalHeaderKey("Docker-Upload-UUID")) uploadUUID = resp.Header.Get(http.CanonicalHeaderKey("Docker-Upload-UUID"))
return return
} }
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return return
@ -404,12 +405,12 @@ func (r *Repository) monolithicBlobUpload(location, digest string, size int64, d
return parseError(err) return parseError(err)
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusCreated { if resp.StatusCode == http.StatusCreated {
return nil return nil
} }
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
@ -443,12 +444,12 @@ func (r *Repository) DeleteBlob(digest string) error {
return parseError(err) return parseError(err)
} }
defer resp.Body.Close()
if resp.StatusCode == http.StatusAccepted { if resp.StatusCode == http.StatusAccepted {
return nil return nil
} }
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body) b, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err