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

feat(v2): add OnHTTPCodes CallOption #188

Merged
merged 2 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions v2/call_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
package gax

import (
"errors"
"math/rand"
"time"

"google.golang.org/api/googleapi"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -119,6 +121,41 @@ func (r *boRetryer) Retry(err error) (time.Duration, bool) {
return 0, false
}

// OnHTTPCodes returns a Retryer that retries if and only if
// the previous attempt returns a googleapi.Error whose status code is stored in
// cc. Pause times between retries are specified by bo.
//
// bo is only used for its parameters; each Retryer has its own copy.
func OnHTTPCodes(bo Backoff, cc ...int) Retryer {
codes := make(map[int]bool, len(cc))
for _, c := range cc {
codes[c] = true
}

return &httpRetryer{
backoff: bo,
codes: codes,
}
}

type httpRetryer struct {
backoff Backoff
codes map[int]bool
}

func (r *httpRetryer) Retry(err error) (time.Duration, bool) {
var gerr *googleapi.Error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Side note: Maybe we should do the work to wrap these with APIError so gax only opperates on this type directly...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean, can you elaborate? The main sticking point for me is that APIError doesn't have a Code property (we'd need to decide on a transport-agnostic Code type or align on gRPC Codes).

Copy link
Member

@codyoss codyoss May 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I mean in apiary-land we should consider starting to wrap those errors with APIError. I believe APIError has access to the raw googleapis.Error. Might make a PR to wrap things later...

if !errors.As(err, &gerr) {
return 0, false
}

if r.codes[gerr.Code] {
return r.backoff.Pause(), true
}

return 0, false
}

// Backoff implements exponential backoff. The wait time between retries is a
// random value between 0 and the "retry period" - the time between retries. The
// retry period starts at Initial and increases by the factor of Multiplier
Expand Down
21 changes: 21 additions & 0 deletions v2/call_option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ package gax

import (
"context"
"net/http"
"testing"
"time"

"google.golang.org/api/googleapi"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -108,3 +110,22 @@ func TestOnErrorFunc(t *testing.T) {
}
}
}

func TestOnHTTPCodes(t *testing.T) {
apiErr := &googleapi.Error{Code: http.StatusBadGateway}
tests := []struct {
c []int
retry bool
}{
{nil, false},
{[]int{http.StatusConflict}, false},
{[]int{http.StatusConflict, http.StatusBadGateway}, true},
{[]int{http.StatusBadGateway}, true},
}
for _, tst := range tests {
b := OnHTTPCodes(Backoff{}, tst.c...)
if _, retry := b.Retry(apiErr); retry != tst.retry {
t.Errorf("retriable codes: %v, error: %s, retry: %t, want %t", tst.c, apiErr, retry, tst.retry)
}
}
}
41 changes: 41 additions & 0 deletions v2/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,47 @@ func ExampleOnCodes() {
_ = resp // TODO: use resp if err is nil
}

func ExampleOnHTTPCodes() {
ctx := context.Background()
c := &fakeClient{}

retryer := gax.OnHTTPCodes(gax.Backoff{
Initial: time.Second,
Max: 32 * time.Second,
Multiplier: 2,
}, http.StatusBadGateway, http.StatusServiceUnavailable)

performSomeRPCWithRetry := func(ctx context.Context) (*fakeResponse, error) {
for {
resp, err := c.PerformSomeRPC(ctx)
if err != nil {
if delay, shouldRetry := retryer.Retry(err); shouldRetry {
if err := gax.Sleep(ctx, delay); err != nil {
return nil, err
}
continue
}
return nil, err
}
return resp, err
}
}

// It's recommended to set deadlines on RPCs and around retrying. This is
// also usually preferred over setting some fixed number of retries: one
// advantage this has is that backoff settings can be changed independently
// of the deadline, whereas with a fixed number of retries the deadline
// would be a constantly-shifting goalpost.
ctxWithTimeout, cancel := context.WithDeadline(ctx, time.Now().Add(5*time.Minute))
defer cancel()

resp, err := performSomeRPCWithRetry(ctxWithTimeout)
if err != nil {
// TODO: handle err
}
_ = resp // TODO: use resp if err is nil
}

func ExampleBackoff() {
ctx := context.Background()

Expand Down