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

feat(storage): add Writer.ChunkRetryDeadline #5482

Merged
merged 3 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 6 additions & 7 deletions storage/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ import (
)

func TestIndefiniteRetries(t *testing.T) {
if testing.Short() {
t.Skip("A long running test for retries")
}

uploadRoute := "/upload"

Expand Down Expand Up @@ -138,6 +135,9 @@ func TestIndefiniteRetries(t *testing.T) {
maxFileSize := 1 << 20
w.ChunkSize = maxFileSize / 4

// Use a shorter retry deadline to speed up the test.
w.ChunkRetryDeadline = time.Second

for i := 0; i < maxFileSize; {
nowStr := time.Now().Format(time.RFC3339Nano)
n, err := fmt.Fprintf(w, "%s", nowStr)
Expand All @@ -153,10 +153,9 @@ func TestIndefiniteRetries(t *testing.T) {
closeDone <- w.Close()
}()

// Given that the ExponentialBackoff is 30 seconds from a start of 100ms,
// let's wait for a maximum of 5 minutes to account for (2**n) increments
// between [100ms, 30s].
maxWait := 5 * time.Minute
// Given the exponential backoff math and the ChunkRetryDeadline math, use a
// max value of 10s. Experimentally this test usually passes in < 5s.
maxWait := 10 * time.Second
select {
case <-time.After(maxWait):
t.Fatalf("Test took longer than %s to return", maxWait)
Expand Down
21 changes: 21 additions & 0 deletions storage/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"fmt"
"io"
"sync"
"time"
"unicode/utf8"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -76,6 +77,23 @@ type Writer struct {
// ChunkSize must be set before the first Write call.
ChunkSize int

// ChunkRetryDeadline sets a per-chunk retry deadline for multi-chunk
// resumable uploads.
//
// For uploads of larger files, the Writer will attempt to retry if the
// request to upload a particular chunk fails with a transient error.
// If a single chunk has been attempting to upload for longer than this
// deadline and the request fails, it will no longer be retried, and the error
// will be returned to the caller. This is only applicable for files which are
// large enough to require a multi-chunk resumable upload. The default value
// is 32s. Users may want to pick a longer deadline if they are using larger
// values for ChunkSize or if they expect to have a slow or unreliable
// internet connection.
tritone marked this conversation as resolved.
Show resolved Hide resolved
//
// To set a deadline on the entire upload, use context timeout or
// cancellation.
ChunkRetryDeadline time.Duration

// ProgressFunc can be used to monitor the progress of a large write.
// operation. If ProgressFunc is not nil and writing requires multiple
// calls to the underlying service (see
Expand Down Expand Up @@ -127,6 +145,9 @@ func (w *Writer) open() error {
if c := attrs.ContentType; c != "" {
mediaOpts = append(mediaOpts, googleapi.ContentType(c))
}
if w.ChunkRetryDeadline != 0 {
mediaOpts = append(mediaOpts, googleapi.ChunkRetryDeadline(w.ChunkRetryDeadline))
}

go func() {
defer close(w.donec)
Expand Down