Skip to content

Commit

Permalink
Merge pull request #954 from stripe/brandur-stripe-should-retry
Browse files Browse the repository at this point in the history
Support `Stripe-Should-Retry` header
  • Loading branch information
remi-stripe authored Sep 24, 2019
2 parents 1b433bc + 85f6800 commit afcb7c2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
10 changes: 10 additions & 0 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,16 @@ func (s *BackendImplementation) shouldRetry(err error, req *http.Request, resp *
return true
}

// The API may ask us not to retry (e.g. if doing so would be a no-op), or
// advise us to retry (e.g. in cases of lock timeouts). Defer to those
// instructions if given.
if resp.Header.Get("Stripe-Should-Retry") == "false" {
return false
}
if resp.Header.Get("Stripe-Should-Retry") == "true" {
return true
}

// 409 Conflict
if resp.StatusCode == http.StatusConflict {
return true
Expand Down
29 changes: 29 additions & 0 deletions stripe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,35 @@ func TestShouldRetry(t *testing.T) {
0,
))

// `Stripe-Should-Retry: false`
assert.False(t, c.shouldRetry(
nil,
&http.Request{},
&http.Response{
Header: http.Header(map[string][]string{
"Stripe-Should-Retry": {"false"},
}),
// Note we send status 409 here, which would normally be retried
StatusCode: http.StatusConflict,
},
0,
))

// `Stripe-Should-Retry: true`
assert.True(t, c.shouldRetry(
nil,
&http.Request{},
&http.Response{
Header: http.Header(map[string][]string{
"Stripe-Should-Retry": {"true"},
}),
// Note we send status 400 here, which would normally not be
// retried
StatusCode: http.StatusBadRequest,
},
0,
))

// 409 Conflict
assert.True(t, c.shouldRetry(
nil,
Expand Down

0 comments on commit afcb7c2

Please sign in to comment.