Browse Source

refactoring the close http response

tcp_read
Chris Lu 5 years ago
parent
commit
96c1ae8471
  1. 4
      weed/s3api/s3api_object_handlers.go
  2. 10
      weed/util/http_util.go

4
weed/s3api/s3api_object_handlers.go

@ -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 {

10
weed/util/http_util.go

@ -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()
} }
Loading…
Cancel
Save