From a96d4254e9481596e7d719ec87fc4c92231aa73d Mon Sep 17 00:00:00 2001 From: chrislu Date: Wed, 2 Mar 2022 20:15:28 -0800 Subject: [PATCH] filer, s3, volume server: a bit memory optimization --- weed/s3api/s3api_object_handlers.go | 5 ++++- weed/server/common.go | 5 ++++- weed/server/filer_server_handlers_proxy.go | 6 +++++- weed/server/volume_server_handlers_read.go | 5 ++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/weed/s3api/s3api_object_handlers.go b/weed/s3api/s3api_object_handlers.go index f454bfad2..16aab2847 100644 --- a/weed/s3api/s3api_object_handlers.go +++ b/weed/s3api/s3api_object_handlers.go @@ -7,6 +7,7 @@ import ( "encoding/xml" "fmt" "github.com/chrislusf/seaweedfs/weed/security" + "github.com/chrislusf/seaweedfs/weed/util/mem" "io" "net/http" "net/url" @@ -368,7 +369,9 @@ func passThroughResponse(proxyResponse *http.Response, w http.ResponseWriter) (s statusCode = proxyResponse.StatusCode } w.WriteHeader(statusCode) - if n, err := io.Copy(w, proxyResponse.Body); err != nil { + buf := mem.Allocate(128 * 1024) + defer mem.Free(buf) + if n, err := io.CopyBuffer(w, proxyResponse.Body, buf); err != nil { glog.V(1).Infof("passthrough response read %d bytes: %v", n, err) } return statusCode diff --git a/weed/server/common.go b/weed/server/common.go index ba4d13456..4bda31157 100644 --- a/weed/server/common.go +++ b/weed/server/common.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http" + "github.com/chrislusf/seaweedfs/weed/util/mem" "io" "io/fs" "mime/multipart" @@ -361,7 +362,9 @@ func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64 w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10)) } w.WriteHeader(http.StatusPartialContent) - if _, err := io.CopyN(w, sendContent, sendSize); err != nil { + buf := mem.Allocate(128 * 1024) + defer mem.Free(buf) + if _, err := io.CopyBuffer(w, io.LimitReader(sendContent, sendSize), buf); err != nil { http.Error(w, "Internal Error", http.StatusInternalServerError) return } diff --git a/weed/server/filer_server_handlers_proxy.go b/weed/server/filer_server_handlers_proxy.go index b8b28790b..301d609ec 100644 --- a/weed/server/filer_server_handlers_proxy.go +++ b/weed/server/filer_server_handlers_proxy.go @@ -3,6 +3,7 @@ package weed_server import ( "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/util" + "github.com/chrislusf/seaweedfs/weed/util/mem" "io" "math/rand" "net/http" @@ -62,6 +63,9 @@ func (fs *FilerServer) proxyToVolumeServer(w http.ResponseWriter, r *http.Reques w.Header()[k] = v } w.WriteHeader(proxyResponse.StatusCode) - io.Copy(w, proxyResponse.Body) + + buf := mem.Allocate(128 * 1024) + defer mem.Free(buf) + io.CopyBuffer(w, proxyResponse.Body, buf) } diff --git a/weed/server/volume_server_handlers_read.go b/weed/server/volume_server_handlers_read.go index 5ce2278bf..203f6c07d 100644 --- a/weed/server/volume_server_handlers_read.go +++ b/weed/server/volume_server_handlers_read.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "github.com/chrislusf/seaweedfs/weed/storage/types" + "github.com/chrislusf/seaweedfs/weed/util/mem" "io" "mime" "net/http" @@ -101,7 +102,9 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request) } } w.WriteHeader(response.StatusCode) - io.Copy(w, response.Body) + buf := mem.Allocate(128 * 1024) + defer mem.Free(buf) + io.CopyBuffer(w, response.Body, buf) return } else { // redirect