Skip to content

Commit

Permalink
merge pull request
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Mar 30, 2021
1 parent 86aeecf commit 4c88019
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions httpstaticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ type HTTPStaticServer struct {

indexes []IndexFileItem
m *mux.Router
bufPool sync.Pool // use sync.Pool caching buf to reduce gc ratio
}

func NewHTTPStaticServer(root string) *HTTPStaticServer {
Expand All @@ -70,6 +71,9 @@ func NewHTTPStaticServer(root string) *HTTPStaticServer {
Root: root,
Theme: "black",
m: m,
bufPool: sync.Pool{
New: func() interface{} { return make([]byte, 32*1024) },
},
}

go func() {
Expand Down Expand Up @@ -249,16 +253,12 @@ func (s *HTTPStaticServer) hUploadOrMkdir(w http.ResponseWriter, req *http.Reque
http.Error(w, "File create "+err.Error(), http.StatusInternalServerError)
return
}

// Note: very large size file might cause poor performance
// _, copyErr = io.Copy(dst, file)

// use sync.Pool caching buf to reduce gc ratio
bufPool := sync.Pool{
New: func() interface{} { return make([]byte, 32*1024) },
}
buf := bufPool.Get().([]byte)
buf := s.bufPool.Get().([]byte)
defer s.bufPool.Put(buf)
_, copyErr = io.CopyBuffer(dst, file, buf)
defer bufPool.Put(&buf)
dst.Close()
// }
if copyErr != nil {
Expand Down

0 comments on commit 4c88019

Please sign in to comment.