Skip to content

Commit

Permalink
retry on 409 responses
Browse files Browse the repository at this point in the history
  • Loading branch information
r00ta committed Sep 7, 2023
1 parent 5277155 commit 8b71f2b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func (client Client) dispatchRequest(request *http.Request) ([]byte, error) {
// as instructed and retry the request.
if err != nil {
serverError, ok := errors.Cause(err).(ServerError)
if ok && serverError.StatusCode == http.StatusServiceUnavailable {
if ok && (serverError.StatusCode == http.StatusServiceUnavailable || serverError.StatusCode == http.StatusConflict) {
retryTimeInt, errConv := strconv.Atoi(serverError.Header.Get(RetryAfterHeaderName))
if errConv == nil {
select {
Expand Down
16 changes: 16 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ func (suite *ClientSuite) TestClientDispatchRequestRetries503(c *gc.C) {
c.Check(*server.requests, jc.DeepEquals, expectedRequestsContent)
}

func (suite *ClientSuite) TestClientDispatchRequestRetries409(c *gc.C) {
URI := "/some/url/?param1=test"
server := newFlakyServer(URI, 409, NumberOfRetries)
defer server.Close()
client, err := NewAnonymousClient(server.URL, "1.0")
c.Assert(err, jc.ErrorIsNil)
content := "content"
request, err := http.NewRequest("GET", server.URL+URI, io.NopCloser(strings.NewReader(content)))
c.Assert(err, jc.ErrorIsNil)

_, err = client.dispatchRequest(request)

c.Assert(err, jc.ErrorIsNil)
c.Check(*server.nbRequests, gc.Equals, NumberOfRetries+1)
}

func (suite *ClientSuite) TestTLSClientDispatchRequestRetries503NilBody(c *gc.C) {
URI := "/some/path"
server := newFlakyTLSServer(URI, 503, NumberOfRetries)
Expand Down
2 changes: 1 addition & 1 deletion testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func newFlakyServer(uri string, code int, nbFlakyResponses int) *flakyServer {
errorMsg := fmt.Sprintf("Error 404: page not found (expected '%v', got '%v').", uri, request.URL.String())
http.Error(writer, errorMsg, http.StatusNotFound)
} else if nbRequests <= nbFlakyResponses {
if code == http.StatusServiceUnavailable {
if code == http.StatusServiceUnavailable || code == http.StatusConflict {
writer.Header().Set("Retry-After", "0")
}
writer.WriteHeader(code)
Expand Down

0 comments on commit 8b71f2b

Please sign in to comment.