Volume range read use sync.pool (#4422)

This commit is contained in:
zemul 2023-04-24 22:00:48 +08:00 committed by GitHub
parent 18686b7375
commit f352616b7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,6 +15,7 @@ import (
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"google.golang.org/grpc" "google.golang.org/grpc"
@ -30,6 +31,10 @@ import (
var serverStats *stats.ServerStats var serverStats *stats.ServerStats
var startTime = time.Now() var startTime = time.Now()
var writePool = sync.Pool{New: func() interface{} {
return bufio.NewWriterSize(nil, 128*1024)
},
}
func init() { func init() {
serverStats = stats.NewServerStats() serverStats = stats.NewServerStats()
@ -279,8 +284,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) error { func processRangeRequest(r *http.Request, w http.ResponseWriter, totalSize int64, mimeType string, writeFn func(writer io.Writer, offset int64, size int64) error) error {
rangeReq := r.Header.Get("Range") rangeReq := r.Header.Get("Range")
bufferedWriter := bufio.NewWriterSize(w, 128*1024) bufferedWriter := writePool.Get().(*bufio.Writer)
defer bufferedWriter.Flush() bufferedWriter.Reset(w)
defer func() {
bufferedWriter.Flush()
writePool.Put(bufferedWriter)
}()
if rangeReq == "" { if rangeReq == "" {
w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10)) w.Header().Set("Content-Length", strconv.FormatInt(totalSize, 10))