Skip to content

Detect connection closed inside POST handler doing long-poll or SSE #1094

Answered by davidpdrsn
cameronelliott asked this question in Q&A
Discussion options

You must be logged in to vote

Happy to hear you're enjoying axum 😊

If I understand you correctly you don't actually need to do anything. It works automatically thanks to async rust and hyper. If the client closes the connection before the server sends a response, hyper will stop calling poll and drop the response future. That can be demonstrated by doing something like this:

use axum::{routing::get, Router};

#[tokio::main]
async fn main() {
    let app = Router::new().route("/", get(handler));

    axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
        .serve(app.into_make_service())
        .await
        .unwrap();
}

async fn handler() {
    // this will go out of scope either when this function returns (whi…

Replies: 1 comment 2 replies

Comment options

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

@davidpdrsn
Comment options

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

This discussion was converted from issue #1092 on June 15, 2022 20:01.