Skip to content

Commit

Permalink
Code review suggestions applied
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-dheyman committed Oct 3, 2023
1 parent 0ce7e08 commit 438adab
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
6 changes: 2 additions & 4 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"bytes"
"context"
"fmt"
"golang.org/x/exp/slices"
"io"
"math/rand"
"net/http"
Expand All @@ -21,7 +20,6 @@ var random *rand.Rand

var endpointsEligibleForRetry = []string{
loginRequestPath,
queryRequestPath,
tokenRequestPath,
authenticatorRequestPath,
}
Expand Down Expand Up @@ -365,7 +363,7 @@ func (r *retryHTTP) isRetryableError(req *http.Request, res *http.Response, err
if res == nil {
return false, err
}
isRetryableURL := slices.Contains(endpointsEligibleForRetry, req.URL.Path)
isRetryableStatus := slices.Contains(statusCodesEligibleForRetry, res.StatusCode)
isRetryableURL := contains(endpointsEligibleForRetry, req.URL.Path)
isRetryableStatus := contains(statusCodesEligibleForRetry, res.StatusCode)
return isRetryableURL && isRetryableStatus, err
}
9 changes: 9 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,12 @@ type unixTimeProvider struct {
func (utp *unixTimeProvider) currentTime() int64 {
return time.Now().UnixMilli()
}

func contains[T comparable](s []T, e T) bool {
for _, v := range s {
if v == e {
return true
}
}
return false
}
20 changes: 20 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,23 @@ func TestGetFromEnvFailOnMissing(t *testing.T) {
t.Error("should report error when there is missing env parameter")
}
}

type tcContains[T comparable] struct {
arr []T
e T
expected bool
}

func TestContains(t *testing.T) {
performContainsTestcase(tcContains[int]{[]int{1, 2, 3, 5}, 4, false}, t)
performContainsTestcase(tcContains[string]{[]string{"a", "b", "C", "F"}, "C", true}, t)
performContainsTestcase(tcContains[int]{[]int{1, 2, 3, 5}, 2, true}, t)
performContainsTestcase(tcContains[string]{[]string{"a", "b", "C", "F"}, "f", false}, t)
}

func performContainsTestcase[S comparable](tc tcContains[S], t *testing.T) {
result := contains(tc.arr, tc.e)
if result != tc.expected {
t.Errorf("contains failed; arr: %v, e: %v, should be %v but was %v", tc.arr, tc.e, tc.expected, result)
}
}

0 comments on commit 438adab

Please sign in to comment.