Skip to content

Commit 1cf7554

Browse files
committed
Tidy up retry middleward and tests.
Signed-off-by: Tom Wilkie <[email protected]>
1 parent 054d4c5 commit 1cf7554

File tree

2 files changed

+59
-26
lines changed

2 files changed

+59
-26
lines changed

pkg/querier/queryrange/retry.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package queryrange
22

33
import (
44
"context"
5-
"net/http"
65

76
"github.com/go-kit/kit/log"
87
"github.com/go-kit/kit/log/level"
@@ -37,30 +36,25 @@ func NewRetryMiddleware(log log.Logger, maxRetries int) Middleware {
3736
}
3837

3938
func (r retry) Do(ctx context.Context, req *Request) (*APIResponse, error) {
40-
var lastErr error
41-
for tries := 0; tries < r.maxRetries; tries++ {
42-
retries.Observe(float64(tries))
39+
tries := 0
40+
defer func() { retries.Observe(float64(tries)) }()
4341

42+
var lastErr error
43+
for ; tries < r.maxRetries; tries++ {
4444
resp, err := r.next.Do(ctx, req)
4545
if err == nil {
4646
return resp, nil
4747
}
4848

49-
// Retry is we get a HTTP 500 or a non-HTTP error.
49+
// Retry if we get a HTTP 500 or a non-HTTP error.
5050
httpResp, ok := httpgrpc.HTTPResponseFromError(err)
51-
if !ok || (ok && httpResp.Code/100 == 5) {
51+
if !ok || httpResp.Code/100 == 5 {
5252
lastErr = err
5353
level.Error(r.log).Log("msg", "error processing request", "try", tries, "err", err)
5454
continue
5555
}
5656

5757
return nil, err
5858
}
59-
60-
if lastErr != nil {
61-
return nil, lastErr
62-
}
63-
64-
return nil, httpgrpc.Errorf(http.StatusInternalServerError, "Query failed after %d retries.", r.maxRetries)
65-
59+
return nil, lastErr
6660
}

pkg/querier/queryrange/retry_test.go

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,65 @@ package queryrange
33
import (
44
"context"
55
fmt "fmt"
6+
"net/http"
67
"sync/atomic"
78
"testing"
89

910
"github.com/go-kit/kit/log"
1011
"github.com/stretchr/testify/require"
12+
"github.com/weaveworks/common/httpgrpc"
1113
)
1214

1315
func TestRetry(t *testing.T) {
14-
try := int32(0)
16+
var try int32
1517

16-
h := NewRetryMiddleware(log.NewNopLogger(), 5).Wrap(
17-
HandlerFunc(func(_ context.Context, req *Request) (*APIResponse, error) {
18-
if atomic.AddInt32(&try, 1) == 5 {
19-
return &APIResponse{Status: "Hello World"}, nil
20-
}
21-
return nil, fmt.Errorf("fail")
22-
}),
23-
)
24-
25-
resp, err := h.Do(nil, nil)
26-
require.NoError(t, err)
27-
require.Equal(t, &APIResponse{Status: "Hello World"}, resp)
18+
for _, tc := range []struct {
19+
name string
20+
handler Handler
21+
resp *APIResponse
22+
err error
23+
}{
24+
{
25+
name: "retry failures",
26+
handler: HandlerFunc(func(_ context.Context, req *Request) (*APIResponse, error) {
27+
if atomic.AddInt32(&try, 1) == 5 {
28+
return &APIResponse{Status: "Hello World"}, nil
29+
}
30+
return nil, fmt.Errorf("fail")
31+
}),
32+
resp: &APIResponse{Status: "Hello World"},
33+
},
34+
{
35+
name: "don't retry 400s",
36+
handler: HandlerFunc(func(_ context.Context, req *Request) (*APIResponse, error) {
37+
return nil, httpgrpc.Errorf(http.StatusBadRequest, "Bad Request")
38+
}),
39+
err: httpgrpc.Errorf(http.StatusBadRequest, "Bad Request"),
40+
},
41+
{
42+
name: "retry 500s",
43+
handler: HandlerFunc(func(_ context.Context, req *Request) (*APIResponse, error) {
44+
return nil, httpgrpc.Errorf(http.StatusInternalServerError, "Internal Server Error")
45+
}),
46+
err: httpgrpc.Errorf(http.StatusInternalServerError, "Internal Server Error"),
47+
},
48+
{
49+
name: "last error",
50+
handler: HandlerFunc(func(_ context.Context, req *Request) (*APIResponse, error) {
51+
if atomic.AddInt32(&try, 1) == 5 {
52+
return nil, httpgrpc.Errorf(http.StatusBadRequest, "Bad Request")
53+
}
54+
return nil, httpgrpc.Errorf(http.StatusInternalServerError, "Internal Server Error")
55+
}),
56+
err: httpgrpc.Errorf(http.StatusBadRequest, "Bad Request"),
57+
},
58+
} {
59+
t.Run(tc.name, func(t *testing.T) {
60+
try = 0
61+
h := NewRetryMiddleware(log.NewNopLogger(), 5).Wrap(tc.handler)
62+
resp, err := h.Do(nil, nil)
63+
require.Equal(t, tc.err, err)
64+
require.Equal(t, tc.resp, resp)
65+
})
66+
}
2867
}

0 commit comments

Comments
 (0)