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(compute/metadata): fix retry logic to not panic on error #4714

Merged
merged 11 commits into from Sep 2, 2021
9 changes: 5 additions & 4 deletions compute/metadata/metadata.go
Expand Up @@ -312,16 +312,17 @@ func (c *Client) getETag(suffix string) (value, etag string, err error) {
for {
var err error
res, err = c.hc.Do(req)
if err == nil {
break
var code int
if res != nil {
code = res.StatusCode
}
if delay, shouldRetry := retryer.Retry(res.StatusCode, err); shouldRetry {
if delay, shouldRetry := retryer.Retry(code, err); shouldRetry {
if err := gax.Sleep(ctx, delay); err != nil {
return "", "", err
}
continue
}
return "", "", err
break
}
defer res.Body.Close()
if res.StatusCode == http.StatusNotFound {
Expand Down
3 changes: 3 additions & 0 deletions compute/metadata/retry.go
Expand Up @@ -43,6 +43,9 @@ type metadataRetryer struct {
}

func (r *metadataRetryer) Retry(status int, err error) (time.Duration, bool) {
if status == 200 {
codyoss marked this conversation as resolved.
Show resolved Hide resolved
return 0, false
}
retryOk := shouldRetry(status, err)
if !retryOk {
return 0, false
Expand Down
7 changes: 7 additions & 0 deletions compute/metadata/retry_test.go
Expand Up @@ -87,6 +87,13 @@ func TestMetadataRetryer(t *testing.T) {
wantDelay: 0,
wantShouldRetry: false,
},
{
name: "don't retry 200",
code: 200,
err: nil,
wantDelay: 0,
wantShouldRetry: false,
},
}

for _, tc := range tests {
Expand Down