Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Support Stripe-Should-Retry header #853

Merged
merged 1 commit into from
Oct 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/stripe/stripe_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def self.should_retry?(error, method:, num_retries:)
return true if error.is_a?(SocketError)

if error.is_a?(Stripe::StripeError)
# 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.
return false if error.http_headers["stripe-should-retry"] == "false"
return true if error.http_headers["stripe-should-retry"] == "true"

# 409 Conflict
return true if error.http_status == 409

Expand Down
22 changes: 22 additions & 0 deletions test/stripe/stripe_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,28 @@ class StripeClientTest < Test::Unit::TestCase
method: :post, num_retries: 0)
end

should "retry when the `Stripe-Should-Retry` header is `true`" do
headers = StripeResponse::Headers.new(
"Stripe-Should-Retry" => ["true"]
)

# Note we send status 400 here, which would normally not be retried.
assert StripeClient.should_retry?(Stripe::StripeError.new(http_headers: headers,
http_status: 400),
method: :post, num_retries: 0)
end

should "not retry when the `Stripe-Should-Retry` header is `false`" do
headers = StripeResponse::Headers.new(
"Stripe-Should-Retry" => ["false"]
)

# Note we send status 409 here, which would normally be retried.
refute StripeClient.should_retry?(Stripe::StripeError.new(http_headers: headers,
http_status: 409),
method: :post, num_retries: 0)
end

should "retry on a 409 Conflict" do
assert StripeClient.should_retry?(Stripe::StripeError.new(http_status: 409),
method: :post, num_retries: 0)
Expand Down