Add Serve::with_executor and Executor trait for custom task spawning#3704
Open
yulnr wants to merge 3 commits intotokio-rs:mainfrom
Open
Add Serve::with_executor and Executor trait for custom task spawning#3704yulnr wants to merge 3 commits intotokio-rs:mainfrom
yulnr wants to merge 3 commits intotokio-rs:mainfrom
Conversation
95d06b8 to
c284a08
Compare
axum::serve previously hardcoded TokioExecutor for spawning connection tasks and hyper's internal HTTP/2 tasks. Users who needed to instrument or intercept spawned tasks (e.g. for telemetry) had to reimplement the entire serve loop. Add a new axum::serve::Executor trait with a with_executor() builder method on both Serve and WithGracefulShutdown. The default remains TokioExecutor, so existing code is unaffected.
c284a08 to
4fb0c8f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #3703
Motivation
axum::servehardcodesTokioExecutorfor spawning connection tasks and hyper's internal HTTP/2 tasks. This is the one thing that cannot be customized by wrapping axum's API from the outside, users are forced to reimplement the entire serve loop (~150 lines) just to swap the executor.This is needed for use cases like runtime telemetry (e.g. dial9-tokio-telemetry) where spawned tasks must be wrapped to capture scheduling delays and wake events.
Solution
axum::serve::Executortrait for custom task spawningServe::with_executor()andWithGracefulShutdown::with_executor()builder methods that take an ExecutorDefault remains
TokioExecutorso it's fully backward compatible.Design
We define a new
axum::serve::Executortrait rather than reusinghyper::rt::Executor<Fut>directly, because the latter is generic at the trait level and we can't boundEto cover all the internal future types hyper needs. The new trait uses a generic method instead, and a thinHyperExecutor<E>adapter bridges to hyper internally.Testing
serving_with_custom_executorend-to-end test with duplex IOwith_executorin both orderings (before and afterwith_graceful_shutdown)