Skip to content

Commit 85f6800

Browse files
committed
Support Stripe-Should-Retry header
As seen in stripe-node, adds support for the `Stripe-Should-Retry` header which is sent by the API when it explicitly would like us to either retry or _not_ retry. I'll add `Retry-After` separately at some point, but I punted it on it for now given that we're not using it yet. See: stripe/stripe-node#692
1 parent 1b433bc commit 85f6800

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

stripe.go

+10
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,16 @@ func (s *BackendImplementation) shouldRetry(err error, req *http.Request, resp *
576576
return true
577577
}
578578

579+
// The API may ask us not to retry (e.g. if doing so would be a no-op), or
580+
// advise us to retry (e.g. in cases of lock timeouts). Defer to those
581+
// instructions if given.
582+
if resp.Header.Get("Stripe-Should-Retry") == "false" {
583+
return false
584+
}
585+
if resp.Header.Get("Stripe-Should-Retry") == "true" {
586+
return true
587+
}
588+
579589
// 409 Conflict
580590
if resp.StatusCode == http.StatusConflict {
581591
return true

stripe_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,35 @@ func TestShouldRetry(t *testing.T) {
156156
0,
157157
))
158158

159+
// `Stripe-Should-Retry: false`
160+
assert.False(t, c.shouldRetry(
161+
nil,
162+
&http.Request{},
163+
&http.Response{
164+
Header: http.Header(map[string][]string{
165+
"Stripe-Should-Retry": {"false"},
166+
}),
167+
// Note we send status 409 here, which would normally be retried
168+
StatusCode: http.StatusConflict,
169+
},
170+
0,
171+
))
172+
173+
// `Stripe-Should-Retry: true`
174+
assert.True(t, c.shouldRetry(
175+
nil,
176+
&http.Request{},
177+
&http.Response{
178+
Header: http.Header(map[string][]string{
179+
"Stripe-Should-Retry": {"true"},
180+
}),
181+
// Note we send status 400 here, which would normally not be
182+
// retried
183+
StatusCode: http.StatusBadRequest,
184+
},
185+
0,
186+
))
187+
159188
// 409 Conflict
160189
assert.True(t, c.shouldRetry(
161190
nil,

0 commit comments

Comments
 (0)