Skip to content

Commit

Permalink
fix retryable pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjenicek committed Nov 28, 2023
1 parent b514ecb commit a4c4f7f
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package transport

import (
"net/http"
"net/url"
"log"
"time"
"strconv"
Expand All @@ -13,7 +12,7 @@ func Retry(baseTransport http.RoundTripper, maxRetries int) func(http.RoundTripp
return func(next http.RoundTripper) http.RoundTripper {
return RoundTripFunc(func(req *http.Request) (resp *http.Response, err error) {
defer func() {
if err == nil || !isRetryable(err, resp) {
if isRetryable(resp) {
for i := 1; i <= maxRetries; i++ {
wait := backOff(resp, i)

Expand All @@ -31,7 +30,7 @@ func Retry(baseTransport http.RoundTripper, maxRetries int) func(http.RoundTripp

startTime := time.Now()
resp, err = baseTransport.RoundTrip(req)
if err == nil || !isRetryable(err, resp) {
if !isRetryable(resp) {
break
}

Expand Down Expand Up @@ -67,16 +66,11 @@ func backOff(resp *http.Response, attempt int) time.Duration {
return sleep
}

func isRetryable(err error, resp *http.Response) bool {
func isRetryable(resp *http.Response) bool {
if resp == nil {
return false
}

// any error returned from Client.Do will be *url.Error
if _, ok := err.(*url.Error); ok {
return false
}

// 429 Too Many Requests is recoverable. Sometimes the server puts
// Retry-After response header to indicate when the server is will be available again
if resp.StatusCode == http.StatusTooManyRequests {
Expand Down

0 comments on commit a4c4f7f

Please sign in to comment.