Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

receiveFile memory optimization: do not use bytes.buffer but write di… #9415

Merged
merged 1 commit into from
May 16, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions util/cmp/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmp

import (
"bufio"
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
Expand Down Expand Up @@ -199,8 +198,11 @@ func compressFiles(appPath string) (*os.File, string, error) {
// Returns error if checksum doesn't match the one provided in the fileMetadata.
// It is responsibility of the caller to close the returned file.
func receiveFile(ctx context.Context, receiver StreamReceiver, checksum, dst string) (*os.File, error) {
fileBuffer := bytes.Buffer{}
hasher := sha256.New()
file, err := ioutil.TempFile(dst, "")
if err != nil {
return nil, fmt.Errorf("error creating file: %w", err)
}
for {
if ctx != nil {
if err := ctx.Err(); err != nil {
Expand All @@ -218,9 +220,9 @@ func receiveFile(ctx context.Context, receiver StreamReceiver, checksum, dst str
if f == nil {
return nil, fmt.Errorf("stream request file is nil")
}
_, err = fileBuffer.Write(f.Chunk)
_, err = file.Write(f.Chunk)
if err != nil {
return nil, fmt.Errorf("error writing file buffer: %w", err)
return nil, fmt.Errorf("error writing file: %w", err)
}
_, err = hasher.Write(f.Chunk)
if err != nil {
Expand All @@ -231,15 +233,6 @@ func receiveFile(ctx context.Context, receiver StreamReceiver, checksum, dst str
return nil, fmt.Errorf("file checksum validation error")
}

file, err := ioutil.TempFile(dst, "")
if err != nil {
return nil, fmt.Errorf("error creating file: %w", err)
}
_, err = fileBuffer.WriteTo(file)
if err != nil {
closeAndDelete(file)
return nil, fmt.Errorf("error writing file: %w", err)
}
_, err = file.Seek(0, io.SeekStart)
if err != nil {
closeAndDelete(file)
Expand Down