close connection immediately when it is not used anymore

This commit is contained in:
Wenkai Yin 2016-07-14 17:50:25 +08:00
parent 48af6cbfb5
commit 3d558b54b4
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)
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Errorf("Unexpected status code: %d", resp.StatusCode)
dump, _ := httputil.DumpResponse(resp, true)
log.Debugf("response: %q", dump)
return nil, fmt.Errorf("Unexpected status code when getting repository list: %d", resp.StatusCode)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
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)
ra.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
}
defer resp.Body.Close()
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-Type"), "text/plain")
@ -167,7 +167,6 @@ func (ra *RepJobAPI) GetLog() {
return
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Errorf("failed to read reponse body: %v", err)

View File

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

View File

@ -250,6 +250,8 @@ func (c *Checker) projectExist() (exist, canWrite bool, err error) {
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusNotFound {
return
}
@ -259,7 +261,6 @@ func (c *Checker) projectExist() (exist, canWrite bool, err error) {
return
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
@ -328,6 +329,8 @@ func (c *Checker) createProject(isPublic bool) error {
return err
}
defer resp.Body.Close()
// version 0.1.1's reponse code is 200
if resp.StatusCode == http.StatusCreated ||
resp.StatusCode == http.StatusOK {
@ -338,7 +341,6 @@ func (c *Checker) createProject(isPublic bool) error {
return ErrConflict
}
defer resp.Body.Close()
message, err := ioutil.ReadAll(resp.Body)
if err != nil {
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)
return "", err
}
if data != nil {
defer data.Close()
}
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)
return "", err

View File

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

View File

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

View File

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