Skip to content

Commit

Permalink
Merge pull request #5 from stevvooe/dont-allocate-map-on-retry
Browse files Browse the repository at this point in the history
internal/net: use switch instead of map for retry
  • Loading branch information
jkw-statsig authored Aug 26, 2021
2 parents 4d6f3eb + 8205c26 commit 3dc18f0
Showing 1 changed file with 7 additions and 16 deletions.
23 changes: 7 additions & 16 deletions internal/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,19 @@ func (n *Net) postRequestInternal(
err := json.NewDecoder(response.Body).Decode(&out)
return err
} else if retries > 0 {
retry := retryCodes()
if retry(response.StatusCode) {
if shouldRetry(response.StatusCode) {
time.Sleep(time.Duration(backoff) * time.Second)
return n.postRequestInternal(endpoint, in, out, retries-1, backoff*backoffMultiplier)
}
}
return fmt.Errorf("http response error code: %d", response.StatusCode)
}

func retryCodes() func(int) bool {
codes := map[int]bool{
408: true,
500: true,
502: true,
503: true,
504: true,
522: true,
524: true,
599: true,
}
return func(key int) bool {
_, ok := codes[key]
return ok
func shouldRetry(code int) bool {
switch code {
case 408, 500, 502, 503, 504, 522, 524, 599:
return true
default:
return false
}
}

0 comments on commit 3dc18f0

Please sign in to comment.