Skip to content

Commit

Permalink
retry on 408 error
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Apr 27, 2023
1 parent 84de11a commit 330cc51
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func WithRetry(ctx context.Context, lim *rate.Limiter, maxAttempts int, fn func(
time.Sleep(rle.RetryAfter)
continue
} else if errors.As(cbErr, &sce) {
if sce.Code >= http.StatusInternalServerError && sce.Code <= 599 {
if isRecoverable(sce.Code) {
// possibly transient error
delay := waitFn(attempt)
tracelogf(ctx, "info", "got server error %d, sleeping %s", sce.Code, delay)
Expand All @@ -88,6 +88,11 @@ func WithRetry(ctx context.Context, lim *rate.Limiter, maxAttempts int, fn func(
return nil
}

// isRecoverable returns true if the status code is a recoverable error.
func isRecoverable(statusCode int) bool {
return (statusCode >= http.StatusInternalServerError && statusCode <= 599) || statusCode == 408
}

// cubicWait is the wait time function. Time is calculated as (x+2)^3 seconds,
// where x is the current attempt number. The maximum wait time is capped at 5
// minutes.
Expand Down
30 changes: 30 additions & 0 deletions internal/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,33 @@ func Test_cubicWait(t *testing.T) {
})
}
}

func Test_isRecoverable(t *testing.T) {
type args struct {
statusCode int
}
tests := []struct {
name string
args args
want bool
}{
{"500", args{500}, true},
{"502", args{502}, true},
{"503", args{503}, true},
{"504", args{504}, true},
{"598", args{598}, true},
{"599", args{599}, true},
{"200", args{200}, false},
{"400", args{400}, false},
{"404", args{404}, false},
{"408", args{408}, true},
{"429", args{429}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := isRecoverable(tt.args.statusCode); got != tt.want {
t.Errorf("isRecoverable() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 330cc51

Please sign in to comment.