Skip to content

Commit

Permalink
Enable http timeout errors to be retried. (#4415)
Browse files Browse the repository at this point in the history
  • Loading branch information
spew authored and rileykarson committed Sep 9, 2019
1 parent 2cda04a commit f160b35
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
13 changes: 12 additions & 1 deletion google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ func retryTimeDuration(retryFunc func() error, duration time.Duration, errorRetr
if err == nil {
return nil
}
for _, e := range errwrap.GetAllType(err, &googleapi.Error{}) {
for _, e := range getAllTypes(err, &googleapi.Error{}, &url.Error{}) {
if isRetryableError(e, errorRetryPredicates) {
return resource.RetryableError(e)
}
Expand All @@ -370,6 +370,17 @@ func retryTimeDuration(retryFunc func() error, duration time.Duration, errorRetr
})
}

func getAllTypes(err error, args ...interface{}) []error {
var result []error
for _, v := range args {
subResult := errwrap.GetAllType(err, v)
if subResult != nil {
result = append(result, subResult...)
}
}
return result
}

func isRetryableError(err error, retryPredicates []func(e error) (bool, string)) bool {

// These operations are always hitting googleapis.com - they should rarely
Expand Down
34 changes: 34 additions & 0 deletions google/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package google

import (
"net/url"
"reflect"
"strings"
"testing"
Expand Down Expand Up @@ -528,3 +529,36 @@ func TestRetryTimeDuration_noretry(t *testing.T) {
t.Errorf("expected error function to be called exactly once, but was called %d times", i)
}
}

type TimeoutError struct {
timeout bool
}

func (e *TimeoutError) Timeout() bool {
return e.timeout
}

func (e *TimeoutError) Error() string {
return "timeout error"
}

func TestRetryTimeDuration_URLTimeoutsShouldRetry(t *testing.T) {
runCount := 0
retryFunc := func() error {
runCount++
if runCount == 1 {
return &url.Error{
Err: &TimeoutError{timeout: true},
}
}
return nil
}
err := retryTimeDuration(retryFunc, 1*time.Minute)
if err != nil {
t.Errorf("unexpected error: got '%v' want 'nil'", err)
}
expectedRunCount := 2
if runCount != expectedRunCount {
t.Errorf("expected the retryFunc to be called %v time(s), instead was called %v time(s)", expectedRunCount, runCount)
}
}

0 comments on commit f160b35

Please sign in to comment.