Skip to content

Commit

Permalink
fix(internal/gensupport): allow user to provide his own retryDeadline…
Browse files Browse the repository at this point in the history
… for uploading (googleapis#685)
  • Loading branch information
agneum committed Oct 2, 2020
1 parent a54a920 commit 2eedfd9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
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

0 comments on commit 2eedfd9

Please sign in to comment.