From 6d3db4445b9fdde09cad21fa707fc297ea9caced Mon Sep 17 00:00:00 2001 From: chrislu Date: Mon, 7 Mar 2022 01:56:47 -0800 Subject: [PATCH] buffer for all range requests --- weed/server/common.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/weed/server/common.go b/weed/server/common.go index 4bda31157..0d458c9c3 100644 --- a/weed/server/common.go +++ b/weed/server/common.go @@ -1,12 +1,12 @@ package weed_server import ( + "bufio" "bytes" "encoding/json" "errors" "fmt" xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http" - "github.com/chrislusf/seaweedfs/weed/util/mem" "io" "io/fs" "mime/multipart" @@ -278,10 +278,12 @@ func adjustHeaderContentDisposition(w http.ResponseWriter, r *http.Request, file func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64, mimeType string, writeFn func(writer io.Writer, offset int64, size int64) error) { rangeReq := r.Header.Get("Range") + bufferedWriter := bufio.NewWriterSize(w, 128*1024) + defer bufferedWriter.Flush() if rangeReq == "" { w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10)) - if err := writeFn(w, 0, totalSize); err != nil { + if err := writeFn(bufferedWriter, 0, totalSize); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -322,7 +324,7 @@ func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64 w.Header().Set("Content-Range", ra.contentRange(totalSize)) w.WriteHeader(http.StatusPartialContent) - err = writeFn(w, ra.start, ra.length) + err = writeFn(bufferedWriter, ra.start, ra.length) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -362,9 +364,7 @@ func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64 w.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10)) } w.WriteHeader(http.StatusPartialContent) - buf := mem.Allocate(128 * 1024) - defer mem.Free(buf) - if _, err := io.CopyBuffer(w, io.LimitReader(sendContent, sendSize), buf); err != nil { + if _, err := io.CopyN(bufferedWriter, sendContent, sendSize); err != nil { http.Error(w, "Internal Error", http.StatusInternalServerError) return }