Skip to content

Commit aa4681e

Browse files
committed
Replace ioutil.ReadAll with io.Copy
Due to the fact that ioutil.ReadAll read the whole file into memory can a big file upload create some OOM kills. io.Copy uses buffers with 32k which should not create OOM
1 parent a6959b9 commit aa4681e

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

upload.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package upload
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"os"
88

@@ -118,7 +118,7 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
118118
r.Body = http.MaxBytesReader(w, r.Body, u.MaxFilesize)
119119
if max_size_err := r.ParseMultipartForm(u.MaxFilesize); max_size_err != nil {
120120
u.logger.Error("ServeHTTP",
121-
zap.String("Request uuid", requuid),
121+
zap.String("requuid", requuid),
122122
zap.String("message", "The uploaded file is too big. Please choose an file that's less than MaxFilesize."),
123123
zap.String("MaxFilesize", humanize.Bytes(uint64(u.MaxFilesize))),
124124
zap.Error(max_size_err),
@@ -132,7 +132,7 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
132132
file, handler, ff_err := r.FormFile("myFile")
133133
if ff_err != nil {
134134
u.logger.Error("FormFile Error",
135-
zap.String("Request uuid", requuid),
135+
zap.String("requuid", requuid),
136136
zap.String("message", "Error Retrieving the File"),
137137
zap.Error(ff_err),
138138
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
@@ -146,7 +146,7 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
146146

147147
if tmpf_err != nil {
148148
u.logger.Error("TempFile Error",
149-
zap.String("Request uuid", requuid),
149+
zap.String("requuid", requuid),
150150
zap.String("message", "Error at TempFile"),
151151
zap.Error(tmpf_err),
152152
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
@@ -156,22 +156,24 @@ func (u Upload) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyhttp
156156

157157
// read all of the contents of our uploaded file into a
158158
// byte array
159-
fileBytes, io_err := ioutil.ReadAll(file)
159+
//fileBytes, io_err := ioutil.ReadAll(file)
160+
fileBytes, io_err := io.Copy(tempFile, file)
160161
if io_err != nil {
161-
u.logger.Error("ReadAll Error",
162-
zap.String("Request uuid", requuid),
163-
zap.String("message", "Error at ReadAll"),
162+
u.logger.Error("Copy Error",
163+
zap.String("requuid", requuid),
164+
zap.String("message", "Error at io.Copy"),
164165
zap.Error(io_err),
165166
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
166167
return caddyhttp.Error(http.StatusInternalServerError, io_err)
167168
}
168169
// write this byte array to our temporary file
169-
tempFile.Write(fileBytes)
170+
//tempFile.Write(fileBytes)
170171

171172
u.logger.Info("Successful Upload Info",
172-
zap.String("Request uuid", requuid),
173+
zap.String("requuid", requuid),
173174
zap.String("Uploaded File", handler.Filename),
174175
zap.Int64("File Size", handler.Size),
176+
zap.Int64("written-bytes", fileBytes),
175177
zap.Any("MIME Header", handler.Header),
176178
zap.Object("request", caddyhttp.LoggableHTTPRequest{Request: r}))
177179

0 commit comments

Comments
 (0)