Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add retry engine #184

Merged
merged 10 commits into from
Mar 22, 2022
Merged

Add retry engine #184

merged 10 commits into from
Mar 22, 2022

Conversation

arlyon
Copy link
Owner

@arlyon arlyon commented Mar 17, 2022

This PR adds the ability to have the client automatically apply retries by applying a RequestStrategy. Closes #178

  • refactor common client code into single struct
  • add idempotency strategy
  • add retries strategy
  • port async-std client
  • port tokio and tokio-blocking clients
  • restore set_stripe_account api
  • determine time-out function
  • fill out documentation
  • add examples
  • clone requests correctly

There are 4 strategies:

Once

This is the basic usage (and the default). This strategy will attempt to make a request, and immediately return control to the caller when it fails.

Idempotent

This is one step up from Once. Using this strategy will use a idempotency key of your choice, and make requests with it. Failures will continue to return control to the caller, however you may retry the request at will.

Retry (not recommended)

This strategy will make the same request up to n times, immediately one-after-the-other. This strategy is not recommended for production use as it is bad manners. Regardless, it may be useful for testing.

Exponential Back-off

This strategy inherits all the semantics of the Retry strategy, but with an increasing wait between requests.

🚨 Note: in both the Retry and ExponentialBackoff strategies n is the maximum limit. If the stripe API explicitly communicates to the client that a retry is not useful (via the Stripe-Should-Retry header), the request will be aborted without reaching the limit.

Usage

// create a client, and use the ExponentialBackoff strategy for all subsequent requests
let client = Client::new("sk_1234").with_strategy(RequestStrategy::ExponentialBackoff(5));

// we need manual control, let's make a new client and use the idempotent strategy
let idempotent = client.clone().with_strategy(RequestStrategy::Idempotent("my_token"));

@arlyon
Copy link
Owner Author

arlyon commented Mar 17, 2022

@FL33TW00D ^

Copy link
Contributor

@FL33TW00D FL33TW00D left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really clean, few things failing but apart from that.

@@ -12,8 +12,6 @@ use crate::error::{ErrorResponse, StripeError};
use crate::params::{AppInfo, Headers};
use crate::resources::ApiVersion;

static USER_AGENT: &str = concat!("Stripe/v3 RustBindings/", env!("CARGO_PKG_VERSION"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this intentionally?

@arlyon
Copy link
Owner Author

arlyon commented Mar 18, 2022

Missing some final issues around cloning requests. Basically cloning an http-types::Request truncates the body, so you need to re-add it. Not a tough fix but it'll have to wait for the moment!

@codecov
Copy link

codecov bot commented Mar 22, 2022

Codecov Report

Merging #184 (cc05fda) into master (650280b) will increase coverage by 1.70%.
The diff coverage is 85.75%.

@@            Coverage Diff            @@
##           master    #184      +/-   ##
=========================================
+ Coverage    4.34%   6.04%   +1.70%     
=========================================
  Files         122     124       +2     
  Lines       10551   10624      +73     
=========================================
+ Hits          458     642     +184     
+ Misses      10093    9982     -111     
Impacted Files Coverage Δ
src/error.rs 0.00% <0.00%> (ø)
src/resources/charge_ext.rs 0.00% <ø> (ø)
src/resources/customer_ext.rs 0.00% <ø> (ø)
src/resources/generated/account.rs 1.86% <ø> (ø)
src/resources/generated/account_link.rs 0.00% <ø> (ø)
src/resources/generated/application_fee.rs 0.00% <ø> (ø)
src/resources/generated/balance.rs 0.00% <ø> (ø)
src/resources/generated/balance_transaction.rs 0.00% <ø> (ø)
src/resources/generated/billing_portal_session.rs 0.00% <ø> (ø)
src/resources/generated/charge.rs 0.67% <ø> (ø)
... and 59 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 650280b...cc05fda. Read the comment docs.

@arlyon arlyon merged commit a18590e into master Mar 22, 2022
@arlyon arlyon deleted the feat/retries branch April 2, 2022 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lots of unreleased code, any plans for a deployment to crates soon?
2 participants