Skip to content

Commit

Permalink
Merge branch 'aeoluswing-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Mar 30, 2021
2 parents f706365 + 4c88019 commit 47789ee
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion httpstaticserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"time"

"regexp"
Expand Down Expand Up @@ -53,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 @@ -69,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 @@ -248,7 +253,12 @@ func (s *HTTPStaticServer) hUploadOrMkdir(w http.ResponseWriter, req *http.Reque
http.Error(w, "File create "+err.Error(), http.StatusInternalServerError)
return
}
_, copyErr = io.Copy(dst, file)

// Note: very large size file might cause poor performance
// _, copyErr = io.Copy(dst, file)
buf := s.bufPool.Get().([]byte)
defer s.bufPool.Put(buf)
_, copyErr = io.CopyBuffer(dst, file, buf)
dst.Close()
// }
if copyErr != nil {
Expand Down

0 comments on commit 47789ee

Please sign in to comment.