Skip to content

Commit e873973

Browse files
authored
Add smart tus-chunksize (#197)
Set chunksize to min of filesize & default chunksize to decrease memory usage.
1 parent d4ebd01 commit e873973

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

scenario/elasticuploadapp.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,18 +122,14 @@ func (settings ElasticUploadAppSettings) Execute(sessionState *session.State, ac
122122
var postApp session.RestRequest
123123
switch settings.Mode {
124124
case Tus:
125-
tempFileClient, err := tempcontent.NewTUSClient(sessionState, connection, settings.ChunkSize, settings.MaxRetries)
126-
if err != nil {
127-
actionState.AddErrors(errors.WithStack(err))
128-
return
129-
}
130125
uploadCtx := sessionState.BaseContext()
131126
if settings.TimeOut > 0 {
132127
ctx, cancel := context.WithTimeout(uploadCtx, time.Duration(settings.TimeOut))
133128
uploadCtx = ctx
134129
defer cancel()
135130
}
136-
tempFile, err := tempFileClient.UploadFromFile(uploadCtx, file)
131+
tempFile, err := tempcontent.UploadTempContentFromFile(uploadCtx, sessionState,
132+
connection, file, settings.ChunkSize, settings.MaxRetries)
137133
if err != nil {
138134
actionState.AddErrors(errors.WithStack(err))
139135
return

scenario/uploaddata.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,6 @@ func (settings UploadDataSettings) Execute(
102102
return
103103
}
104104

105-
tempFileClient, err := tempcontent.NewTUSClient(sessionState, connection, settings.ChunkSize, settings.MaxRetries)
106-
if err != nil {
107-
actionState.AddErrors(errors.WithStack(err))
108-
return
109-
}
110-
111105
uploadCtx := sessionState.BaseContext()
112106
if settings.TimeOut > 0 {
113107
ctx, cancel := context.WithTimeout(uploadCtx, time.Duration(settings.TimeOut))
@@ -120,9 +114,11 @@ func (settings UploadDataSettings) Execute(
120114
return
121115
}
122116
defer file.Close()
123-
tempFile, err := tempFileClient.UploadFromFile(uploadCtx, file)
117+
118+
tempFile, err := tempcontent.UploadTempContentFromFile(uploadCtx, sessionState,
119+
connection, file, settings.ChunkSize, settings.MaxRetries)
124120
if err != nil {
125-
actionState.AddErrors(errors.Wrap(err, "failed to upload temp content from file"))
121+
actionState.AddErrors(errors.WithStack(err))
126122
return
127123
}
128124

tempcontent/tus.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,39 @@ type (
2828
URL string
2929
}
3030

31-
TUSClient struct {
31+
tusClient struct {
3232
tusClient *tus.Client
3333
maxRetries int
3434
}
3535
)
3636

37-
func NewTUSClient(sessionState *session.State, connection *connection.ConnectionSettings, chunkSize int64, maxRetries int) (*TUSClient, error) {
37+
// UploadTempContentFromFile uploads a tempfile with smart selection of
38+
// chunksize. If chunksize is not set (<=0) and file size is smaller than
39+
// default chunksize, the chunksize it will be set to the file size roofed to
40+
// closest 1024 bytes.
41+
func UploadTempContentFromFile(ctx context.Context, sessionState *session.State, connection *connection.ConnectionSettings,
42+
file *os.File, chunkSize int64, maxRetries int) (*RemoteFile, error) {
43+
44+
fileStat, err := file.Stat()
45+
if err != nil {
46+
return nil, errors.WithStack(err)
47+
}
48+
fileSize := fileStat.Size()
49+
if chunkSize <= 0 && fileSize < defaultChunkSize {
50+
chunkSize = (chunkSize/1024 + 1) * 1024
51+
}
52+
tempFileClient, err := newTUSClient(sessionState, connection, chunkSize, maxRetries)
53+
if err != nil {
54+
return nil, errors.WithStack(err)
55+
}
56+
tempFile, err := tempFileClient.uploadFromFile(ctx, file)
57+
if err != nil {
58+
return nil, errors.Wrap(err, "failed to upload temp content from file")
59+
}
60+
return tempFile, nil
61+
}
62+
63+
func newTUSClient(sessionState *session.State, connection *connection.ConnectionSettings, chunkSize int64, maxRetries int) (*tusClient, error) {
3864
if maxRetries < 0 {
3965
maxRetries = 0
4066
}
@@ -61,13 +87,13 @@ func NewTUSClient(sessionState *session.State, connection *connection.Connection
6187
if err != nil {
6288
return nil, errors.Wrap(err, "failed to create tus client")
6389
}
64-
return &TUSClient{
90+
return &tusClient{
6591
tusClient: client,
6692
maxRetries: maxRetries,
6793
}, nil
6894
}
6995

70-
func (client TUSClient) UploadFromFile(ctx context.Context, file *os.File) (*RemoteFile, error) {
96+
func (client tusClient) uploadFromFile(ctx context.Context, file *os.File) (*RemoteFile, error) {
7197
upload, err := tus.NewUploadFromFile(file)
7298
if err != nil {
7399
return nil, errors.Wrap(err, "failed to create tus upload from file")

0 commit comments

Comments
 (0)