Skip to content

Commit ff6d014

Browse files
authored
Merge pull request #139 from ash2k/ash2k/cleanups
Various small cleanups
2 parents 789d441 + dbd0367 commit ff6d014

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

client.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ func baseRetryPolicy(resp *http.Response, err error) (bool, error) {
465465
// the server time to recover, as 500's are typically not permanent
466466
// errors and may relate to outages on the server side. This will catch
467467
// invalid response codes as well, like 0 and 999.
468-
if resp.StatusCode == 0 || (resp.StatusCode >= 500 && resp.StatusCode != 501) {
468+
if resp.StatusCode == 0 || (resp.StatusCode >= 500 && resp.StatusCode != http.StatusNotImplemented) {
469469
return true, fmt.Errorf("unexpected HTTP status %s", resp.Status)
470470
}
471471

@@ -570,8 +570,6 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
570570
for i := 0; ; i++ {
571571
attempt++
572572

573-
var code int // HTTP response code
574-
575573
// Always rewind the request body when non-nil.
576574
if req.body != nil {
577575
body, err := req.body()
@@ -599,9 +597,6 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
599597

600598
// Attempt the request
601599
resp, doErr = c.HTTPClient.Do(req.Request)
602-
if resp != nil {
603-
code = resp.StatusCode
604-
}
605600

606601
// Check if we should continue with retries.
607602
shouldRetry, checkErr = c.CheckRetry(req.Context(), resp, doErr)
@@ -646,23 +641,25 @@ func (c *Client) Do(req *Request) (*http.Response, error) {
646641
}
647642

648643
wait := c.Backoff(c.RetryWaitMin, c.RetryWaitMax, i, resp)
649-
desc := fmt.Sprintf("%s %s", req.Method, req.URL)
650-
if code > 0 {
651-
desc = fmt.Sprintf("%s (status: %d)", desc, code)
652-
}
653644
if logger != nil {
645+
desc := fmt.Sprintf("%s %s", req.Method, req.URL)
646+
if resp != nil {
647+
desc = fmt.Sprintf("%s (status: %d)", desc, resp.StatusCode)
648+
}
654649
switch v := logger.(type) {
655650
case LeveledLogger:
656651
v.Debug("retrying request", "request", desc, "timeout", wait, "remaining", remain)
657652
case Logger:
658653
v.Printf("[DEBUG] %s: retrying in %s (%d left)", desc, wait, remain)
659654
}
660655
}
656+
timer := time.NewTimer(wait)
661657
select {
662658
case <-req.Context().Done():
659+
timer.Stop()
663660
c.HTTPClient.CloseIdleConnections()
664661
return nil, req.Context().Err()
665-
case <-time.After(wait):
662+
case <-timer.C:
666663
}
667664

668665
// Make shallow copy of http Request so that we can modify its body

0 commit comments

Comments
 (0)