Skip to content

Detect client disconnection with body::Sender #2917

@losfair

Description

@losfair

Describe the solution you'd like

I'm using hyper::body::Sender to send an SSE stream to the client. For each connection there is an associated Tokio task that subscribes to a distributed message queue, and I would like to stop the task on client disconnection. It seems that currently there isn't a method on Sender that allows to detect disconnection without attempting to send a message.

I think an API like this would work:

impl Sender {
  pub async fn wait_disconnect(&mut self) -> Result<()>;
}

Describe alternatives you've considered

  1. Send heartbeat messages to the client periodically and stop the task on failure.
  2. Periodically call poll_ready.

I'm already doing 1 anyway, but this adds latency between the client closing the connection and the server stopping the task, even when the connection is gracefully shut down.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-featureCategory: feature. This is adding a new feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions