buffer for all range requests

This commit is contained in:
chrislu 2022-03-07 01:56:47 -08:00
parent f3bcbeb60a
commit 6d3db4445b

View file

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