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

fix(internal/gensupport): allow user to provide his own retryDeadline for uploading (#685) #686

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions internal/gensupport/resumable.go
Expand Up @@ -24,8 +24,7 @@ type Backoff interface {

// These are declared as global variables so that tests can overwrite them.
var (
retryDeadline = 32 * time.Second
backoff = func() Backoff {
backoff = func() Backoff {
return &gax.Backoff{Initial: 100 * time.Millisecond}
}
// isRetryable is a platform-specific hook, specified in retryable_linux.go
Expand All @@ -38,6 +37,12 @@ const (
// should be retried.
// https://cloud.google.com/storage/docs/json_api/v1/status-codes#standardcodes
statusTooManyRequests = 429

// retryDeadlineKey defines a context key to override the default retry deadline.
retryDeadlineKey = "retryDeadline"

// defaultRetryDeadline defines the default deadline duration for a single chunk uploading.
defaultRetryDeadline = 32 * time.Second
)

// ResumableUpload is used by the generated APIs to provide resumable uploads.
Expand Down Expand Up @@ -177,13 +182,18 @@ func (rx *ResumableUpload) Upload(ctx context.Context) (resp *http.Response, err
return resp, nil
}

chunkDeadline := defaultRetryDeadline
if retryDeadline, ok := ctx.Value(retryDeadlineKey).(time.Duration); ok {
chunkDeadline = retryDeadline
}

// Send all chunks.
for {
var pause time.Duration

// Each chunk gets its own initialized-at-zero retry.
bo := backoff()
quitAfter := time.After(retryDeadline)
quitAfter := time.After(chunkDeadline)

// Retry loop for a single chunk.
for {
Expand Down
6 changes: 1 addition & 5 deletions internal/gensupport/resumable_test.go
Expand Up @@ -337,17 +337,13 @@ func TestRetry_EachChunkHasItsOwnRetryDeadline(t *testing.T) {
Callback: func(int64) {},
}

oldRetryDeadline := retryDeadline
retryDeadline = 5 * time.Second
defer func() { retryDeadline = oldRetryDeadline }()

oldBackoff := backoff
backoff = func() Backoff { return new(PauseOneSecond) }
defer func() { backoff = oldBackoff }()

resCode := make(chan int, 1)
go func() {
resp, err := rx.Upload(context.Background())
resp, err := rx.Upload(context.WithValue(context.Background(), retryDeadlineKey, 5*time.Second))
if err != nil {
t.Error(err)
return
Expand Down