Skip to content
Discussion options

You must be logged in to vote

Yep, the misunderstanding is around what Tokio can preempt.

Tokio tasks only yield at .await points. serde_json::from_str(...) is fully synchronous CPU work, so while it’s parsing a huge payload it keeps that runtime worker busy until parsing finishes. If a few requests do that at the same time, they can occupy all worker threads and the rest of the server starts starving — including /health.

Move the CPU-heavy part off the async worker threads:

pub async fn process_data_handler() -> Result<impl IntoResponse, StatusCode> {
    let url = "https://api.example.com/massive_dataset.json";

    let response = reqwest::get(url)
        .await
        .map_err(|_| StatusCode::BAD_GATEWAY)?
        .

Replies: 3 comments 4 replies

Comment options

You must be logged in to vote
1 reply
@ddroid
Comment options

Comment options

You must be logged in to vote
1 reply
@ddroid
Comment options

Comment options

You must be logged in to vote
2 replies
@ddroid
Comment options

@ddroid
Comment options

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