Skip to content

Commit

Permalink
handlers/objects: Optimize buffer allocation for multipart form file
Browse files Browse the repository at this point in the history
Continues f1f7ab4 for
`UploadContainerObject`. Size of the file from the multipart form is at
hand, so using it to allocate a buffer will improve performance on small
volumes.

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Jun 25, 2024
1 parent 765d547 commit 327445c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions handlers/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -1036,8 +1036,15 @@ func (a *RestAPI) UploadContainerObject(ctx echo.Context, containerID apiserver.
return ctx.JSON(http.StatusBadRequest, resp)
}

chunk := make([]byte, a.payloadBufferSize)
_, err = io.CopyBuffer(writer, file, chunk)
var buf []byte
if header.Size > 0 && uint64(header.Size) < a.payloadBufferSize {
buf = make([]byte, header.Size)
} else {
// Size field is not documented, so we cannot be sure what exactly non-positive
// values mean. Thus, it's better to keep default behavior for them.
buf = make([]byte, a.payloadBufferSize)
}
_, err = io.CopyBuffer(writer, file, buf)
if err != nil {
resp := a.logAndGetErrorResponse("write", err)
return ctx.JSON(http.StatusBadRequest, resp)
Expand Down

0 comments on commit 327445c

Please sign in to comment.