Skip to content

Commit

Permalink
feat(storage): configurable retries for uploads
Browse files Browse the repository at this point in the history
Depends on merge and release of googleapis/google-api-go-client#1324
  • Loading branch information
tritone committed Dec 14, 2021
1 parent 2c664a6 commit 1d1fe4d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 additions & 0 deletions storage/writer.go
Expand Up @@ -172,6 +172,25 @@ func (w *Writer) open() error {
// call to set up the upload as well as calls to upload individual chunks
// for a resumable upload (as long as the chunk size is non-zero). Hence
// there is no need to add retries here.

// Retry only when the operation is idempotent or the retry policy is RetryAlways.
var isIdempotent bool
if w.o.conds != nil && (w.o.conds.GenerationMatch >= 0 || w.o.conds.DoesNotExist == true) {
isIdempotent = true
}
var useRetry bool
if (w.o.retry == nil || w.o.retry.policy == RetryIdempotent) && isIdempotent {
useRetry = true
} else if w.o.retry != nil && w.o.retry.policy == RetryAlways {
useRetry = true
}
if useRetry {
if w.o.retry != nil {
call.WithRetry(w.o.retry.backoff, w.o.retry.shouldRetry)
} else {
call.WithRetry(nil, nil)
}
}
resp, err = call.Do()
}
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion storage/writer_test.go
Expand Up @@ -36,7 +36,7 @@ func TestErrorOnObjectsInsertCall(t *testing.T) {

doWrite := func(mt *mockTransport) *Writer {
client := mockClient(t, mt)
wc := client.Bucket("bucketname").Object("filename1").NewWriter(ctx)
wc := client.Bucket("bucketname").Object("filename1").If(Conditions{DoesNotExist: true}).NewWriter(ctx)
wc.ContentType = "text/plain"

// We can't check that the Write fails, since it depends on the write to the
Expand Down

0 comments on commit 1d1fe4d

Please sign in to comment.