Skip to content

Commit

Permalink
Fail fast if context errors out
Browse files Browse the repository at this point in the history
Signed-off-by: 🌲 Harry 🌊 John 🏔 <[email protected]>
  • Loading branch information
harry671003 committed Sep 14, 2023
1 parent b514ae6 commit 26c024e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
7 changes: 5 additions & 2 deletions pkg/frontend/transport/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewRetry(maxRetries int, reg prometheus.Registerer) *Retry {
}
}

func (r *Retry) Do(f func() (*httpgrpc.HTTPResponse, error)) (*httpgrpc.HTTPResponse, error) {
func (r *Retry) Do(ctx context.Context, f func() (*httpgrpc.HTTPResponse, error)) (*httpgrpc.HTTPResponse, error) {
if r.maxRetries == 0 {
// Retries are disabled. Try only once.
return f()
Expand All @@ -39,8 +39,11 @@ func (r *Retry) Do(f func() (*httpgrpc.HTTPResponse, error)) (*httpgrpc.HTTPResp
err error
)
for ; tries < r.maxRetries; tries++ {
resp, err = f()
if ctx.Err() != nil {
return nil, ctx.Err()
}

resp, err = f()
if err != nil && err != context.Canceled {
continue // Retryable
} else if resp != nil && resp.Code/100 == 5 {
Expand Down
13 changes: 7 additions & 6 deletions pkg/frontend/transport/retry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package transport

import (
"context"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -11,18 +12,18 @@ import (
func TestRetry(t *testing.T) {
tries := atomic.NewInt64(3)
r := NewRetry(3, nil)

res, err := r.Do(func() (*httpgrpc.HTTPResponse, error) {
ctx := context.Background()
res, err := r.Do(ctx, func() (*httpgrpc.HTTPResponse, error) {
try := tries.Dec()
if try > 1 {
return &httpgrpc.HTTPResponse{
Code: 500,
}, nil
} else {
return &httpgrpc.HTTPResponse{
Code: 200,
}, nil
}
return &httpgrpc.HTTPResponse{
Code: 200,
}, nil

})

require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/frontend/v1/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func (f *Frontend) RoundTripGRPC(ctx context.Context, req *httpgrpc.HTTPRequest)
}
}

return f.retry.Do(func() (*httpgrpc.HTTPResponse, error) {
return f.retry.Do(ctx, func() (*httpgrpc.HTTPResponse, error) {
request := request{
request: req,
originalCtx: ctx,
Expand Down
2 changes: 1 addition & 1 deletion pkg/frontend/v2/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (f *Frontend) RoundTripGRPC(ctx context.Context, req *httpgrpc.HTTPRequest)
ctx, cancel := context.WithCancel(ctx)
defer cancel()

return f.retry.Do(func() (*httpgrpc.HTTPResponse, error) {
return f.retry.Do(ctx, func() (*httpgrpc.HTTPResponse, error) {
freq := &frontendRequest{
queryID: f.lastQueryID.Inc(),
request: req,
Expand Down

0 comments on commit 26c024e

Please sign in to comment.