Skip to content

Commit

Permalink
fix: #89
Browse files Browse the repository at this point in the history
Signed-off-by: Yoan Blanc <yblanc@edgelab.ch>
  • Loading branch information
greut committed Sep 16, 2020
1 parent 3efdc7f commit c6b32d9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
37 changes: 20 additions & 17 deletions httpclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,26 @@ outter:
response.Body.Close()
}

// Wait before retrying.
if i > 0 {
backoffTime := c.retrier.NextInterval(i - 1)
ctx, cancel := context.WithTimeout(context.Background(), backoffTime)

select {
case <-ctx.Done():
cancel()

case <-request.Context().Done():
cancel()

multiErr.Push(request.Context().Err().Error())
c.reportError(request, request.Context().Err())

// If the request context has already been cancelled, don't retry
break outter
}
}

c.reportRequestStart(request)
var err error
response, err = c.client.Do(request)
Expand All @@ -166,23 +186,6 @@ outter:
multiErr = &valkyrie.MultiError{}
break
}

backoffTime := c.retrier.NextInterval(i)
ctx, cancel := context.WithTimeout(context.Background(), backoffTime)

select {
case <-ctx.Done():
cancel()

case <-request.Context().Done():
cancel()

multiErr.Push(request.Context().Err().Error())
c.reportError(request, request.Context().Err())

// If the request context has already been cancelled, don't retry
break outter
}
}

return response, multiErr.HasError()
Expand Down
1 change: 1 addition & 0 deletions httpclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ func TestHTTPClientDontRetryWhenContextIsCancelled(t *testing.T) {
}

w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{ "response": "something went wrong" }`))
}

Expand Down
1 change: 0 additions & 1 deletion httpclient/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,5 @@ func ExampleWithRetrier() {
// Output: retry attempt 0
// retry attempt 1
// retry attempt 2
// retry attempt 3
// error
}

0 comments on commit c6b32d9

Please sign in to comment.