Skip to content

Is the Stripe Client via async-stripe thread safe? Should I use 1 per request, connection, thread/worker, or server? #501

Answered by arlyon
WarrenN1 asked this question in Q&A
Discussion options

You must be logged in to vote

Yep, we don't allow any unsafe code at all, so if the compiler lets you do it then you can.

https://github.com/arlyon/async-stripe/blob/master/src/lib.rs#L57

Since rust is thread safe at the type level, then so is this library. Specifically:

  • all the clients are thread safe, with respect to the Send + Sync bounds of a normal rust async application
  • if you need to make a new client, for a new thread, you can just clone it and move it to that task
  • the same client can make multiple requests concurrently (using, for example, tokio::join), since requests do not require mutable access to the client to execute.

For the purposes of rocket, you could for example create a State. Notice in the docs

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@WarrenN1
Comment options

@seanpianka
Comment options

@arlyon
Comment options

Answer selected by arlyon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants