Skip to content

Commit

Permalink
Nil-safe response status code check (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenny-statsig authored Jun 17, 2024
1 parent 3c9f008 commit 42df398
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (transport *transport) doRequest(
return nil, &TransportError{Err: err}
}
options.fill_defaults()
response, err, retried := retry(options.retries, time.Duration(options.backoff), func() (*http.Response, bool, error) {
response, err, attempts := retry(options.retries, time.Duration(options.backoff), func() (*http.Response, bool, error) {
response, err := transport.client.Do(request)
if err != nil {
return response, response != nil, err
Expand All @@ -213,11 +213,14 @@ func (transport *transport) doRequest(
})

if err != nil {
if response == nil {
return response, &TransportError{Err: err}
}
return response, &TransportError{
RequestMetadata: &RequestMetadata{
StatusCode: response.StatusCode,
Endpoint: endpoint,
Retries: retried,
Retries: attempts,
},
Err: err,
}
Expand All @@ -234,19 +237,19 @@ func (transport *transport) parseResponse(response *http.Response, out interface
}

func retry(retries int, backoff time.Duration, fn func() (*http.Response, bool, error)) (*http.Response, error, int) {
retried := 0
attempts := 0
for {
if response, retry, err := fn(); retry {
if retries <= 0 {
return response, err, retried
return response, err, attempts
}

retries--
retried++
attempts++
time.Sleep(backoff)
backoff = backoff * backoffMultiplier
} else {
return response, err, retried
return response, err, attempts
}
}
}
Expand Down

0 comments on commit 42df398

Please sign in to comment.