refactoring the close http response

This commit is contained in:
Chris Lu 2020-02-14 09:46:36 -08:00
parent cf5064d702
commit 96c1ae8471
2 changed files with 8 additions and 6 deletions

View file

@ -13,6 +13,7 @@ import (
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/server" "github.com/chrislusf/seaweedfs/weed/server"
"github.com/chrislusf/seaweedfs/weed/util"
) )
var ( var (
@ -145,9 +146,10 @@ func (s3a *S3ApiServer) proxyToFiler(w http.ResponseWriter, r *http.Request, des
writeErrorResponse(w, ErrInternalError, r.URL) writeErrorResponse(w, ErrInternalError, r.URL)
return return
} }
defer resp.Body.Close() defer util.CloseResponse(resp)
responseFn(resp, w) responseFn(resp, w)
} }
func passThroughResponse(proxyResonse *http.Response, w http.ResponseWriter) { func passThroughResponse(proxyResonse *http.Response, w http.ResponseWriter) {
for k, v := range proxyResonse.Header { for k, v := range proxyResonse.Header {

View file

@ -88,7 +88,7 @@ func Head(url string) (http.Header, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer closeResp(r) defer CloseResponse(r)
if r.StatusCode >= 400 { if r.StatusCode >= 400 {
return nil, fmt.Errorf("%s: %s", url, r.Status) return nil, fmt.Errorf("%s: %s", url, r.Status)
} }
@ -130,7 +130,7 @@ func GetBufferStream(url string, values url.Values, allocatedBytes []byte, eachB
if err != nil { if err != nil {
return err return err
} }
defer closeResp(r) defer CloseResponse(r)
if r.StatusCode != 200 { if r.StatusCode != 200 {
return fmt.Errorf("%s: %s", url, r.Status) return fmt.Errorf("%s: %s", url, r.Status)
} }
@ -153,7 +153,7 @@ func GetUrlStream(url string, values url.Values, readFn func(io.Reader) error) e
if err != nil { if err != nil {
return err return err
} }
defer closeResp(r) defer CloseResponse(r)
if r.StatusCode != 200 { if r.StatusCode != 200 {
return fmt.Errorf("%s: %s", url, r.Status) return fmt.Errorf("%s: %s", url, r.Status)
} }
@ -262,7 +262,7 @@ func ReadUrlAsStream(fileUrl string, offset int64, size int, fn func(data []byte
if err != nil { if err != nil {
return 0, err return 0, err
} }
defer closeResp(r) defer CloseResponse(r)
if r.StatusCode >= 400 { if r.StatusCode >= 400 {
return 0, fmt.Errorf("%s: %s", fileUrl, r.Status) return 0, fmt.Errorf("%s: %s", fileUrl, r.Status)
} }
@ -308,7 +308,7 @@ func ReadUrlAsReaderCloser(fileUrl string, rangeHeader string) (io.ReadCloser, e
return r.Body, nil return r.Body, nil
} }
func closeResp(resp *http.Response) { func CloseResponse(resp *http.Response) {
io.Copy(ioutil.Discard, resp.Body) io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close() resp.Body.Close()
} }