-
SummaryFirst, thanks to the maintainers for the detailed documentation and examples. This question is born out of curiosity rather than any issue I'm having sharing the database pool in the way prescribed in the documentation (via state extractor). I'm working in a rust mono-repo and have configured other projects in the repo to reference a static ref to a sqlx pg pool (sea_orm) using tokio::sync::OnceCell. The motivation for extending this pattern for my axum project would be pretty minor -- to access the pool in a consistent way with respect to other projects in the mono-repo. It's my understanding that axum serves all concurrent connections from a single runtime, so I believe every handler should be able to reference the static db pool from a common configuration module without the need to define the state extractor in the handler arguments. In the end it seems like I'm sharing a reference across my active connections either way. Am I missing anything? Thanks in advance axum version0.7.4 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I think you're misunderstanding a few things, but your conclusion is correct. First off, both axum and tokio's To your actual question, there is nothing inherently wrong with sharing state with statics instead of axum's |
Beta Was this translation helpful? Give feedback.
I think you're misunderstanding a few things, but your conclusion is correct.
First off, both axum and tokio's
OnceCell
type don't interact with the tokio runtime at all, i.e. they can be used inside any async runtime (axum::serve
does make use ofhyper-util
s tokio integration, but you serve connections and pass them to axum differently). So even if the runtime that runs the async closure to initialize the cell was different from the one that you use to run hyper/axum in, that wouldn't be a problem.To your actual question, there is nothing inherently wrong with sharing state with statics instead of axum's
State
. We recommendState
because it's easy and has the advantage of allowing multi…