mirror of
https://github.com/gogs/gogs.git
synced 2025-05-31 11:42:13 +00:00
Use temporary file to avoid out-of-memory when receiving big chunks. (#3748)
* Use temporary file to avoid out-of-memory when receiving big chunk. Not perfect but I think it's a reasonable solution. For small request bodies, I suppose performance wouldn't be an issue. For large ones, this seems to be a necessary evil. * Must close the open file to avoid fd leaks
This commit is contained in:
parent
42a3bbb0f4
commit
cdedc2d188
@ -364,14 +364,23 @@ func serviceRPC(h serviceHandler, service string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if h.cfg.OnSucceed != nil {
|
if h.cfg.OnSucceed != nil {
|
||||||
input, err = ioutil.ReadAll(reqBody)
|
tmpfile, err := ioutil.TempFile("", "gogs")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.GitLogger.Error(2, "fail to read request body: %v", err)
|
log.GitLogger.Error(2, "fail to create temporary file: %v", err)
|
||||||
|
h.w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer os.Remove(tmpfile.Name())
|
||||||
|
defer tmpfile.Close()
|
||||||
|
|
||||||
|
_, err = io.Copy(tmpfile, reqBody)
|
||||||
|
if err != nil {
|
||||||
|
log.GitLogger.Error(2, "fail to save request body: %v", err)
|
||||||
h.w.WriteHeader(http.StatusInternalServerError)
|
h.w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
br = bytes.NewReader(input)
|
br = tmpfile
|
||||||
} else {
|
} else {
|
||||||
br = reqBody
|
br = reqBody
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user