filer, s3, volume server: a bit memory optimization

This commit is contained in:
chrislu 2022-03-02 20:15:28 -08:00
parent 6fbbc78574
commit a96d4254e9
4 changed files with 17 additions and 4 deletions

View file

@ -7,6 +7,7 @@ import (
"encoding/xml" "encoding/xml"
"fmt" "fmt"
"github.com/chrislusf/seaweedfs/weed/security" "github.com/chrislusf/seaweedfs/weed/security"
"github.com/chrislusf/seaweedfs/weed/util/mem"
"io" "io"
"net/http" "net/http"
"net/url" "net/url"
@ -368,7 +369,9 @@ func passThroughResponse(proxyResponse *http.Response, w http.ResponseWriter) (s
statusCode = proxyResponse.StatusCode statusCode = proxyResponse.StatusCode
} }
w.WriteHeader(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) glog.V(1).Infof("passthrough response read %d bytes: %v", n, err)
} }
return statusCode return statusCode

View file

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http" xhttp "github.com/chrislusf/seaweedfs/weed/s3api/http"
"github.com/chrislusf/seaweedfs/weed/util/mem"
"io" "io"
"io/fs" "io/fs"
"mime/multipart" "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.Header().Set("Content-Length", strconv.FormatInt(sendSize, 10))
} }
w.WriteHeader(http.StatusPartialContent) 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) http.Error(w, "Internal Error", http.StatusInternalServerError)
return return
} }

View file

@ -3,6 +3,7 @@ package weed_server
import ( import (
"github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/glog"
"github.com/chrislusf/seaweedfs/weed/util" "github.com/chrislusf/seaweedfs/weed/util"
"github.com/chrislusf/seaweedfs/weed/util/mem"
"io" "io"
"math/rand" "math/rand"
"net/http" "net/http"
@ -62,6 +63,9 @@ func (fs *FilerServer) proxyToVolumeServer(w http.ResponseWriter, r *http.Reques
w.Header()[k] = v w.Header()[k] = v
} }
w.WriteHeader(proxyResponse.StatusCode) w.WriteHeader(proxyResponse.StatusCode)
io.Copy(w, proxyResponse.Body)
buf := mem.Allocate(128 * 1024)
defer mem.Free(buf)
io.CopyBuffer(w, proxyResponse.Body, buf)
} }

View file

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/chrislusf/seaweedfs/weed/storage/types" "github.com/chrislusf/seaweedfs/weed/storage/types"
"github.com/chrislusf/seaweedfs/weed/util/mem"
"io" "io"
"mime" "mime"
"net/http" "net/http"
@ -101,7 +102,9 @@ func (vs *VolumeServer) GetOrHeadHandler(w http.ResponseWriter, r *http.Request)
} }
} }
w.WriteHeader(response.StatusCode) 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 return
} else { } else {
// redirect // redirect