1

My server uses Actix-web Rust framework. It functions as a proxy, forwarding client requests to a backend (PostgreSQL), and returns the responses.

Due to the nature of my service (map tile server), the client often cancels the GET requests using the browser's fetch(..., {AbortSignal}). I suspect the actual socket connection remains, just one specific fetch is aborted.

How can I catch such request aborts in Actix-web, and notify my backend to stop handling it as well?

Hypothetical code:

/// Run a backend query, and if the client aborts early,
/// make sure to abort the backend query too
#[route("/foo")]
async fn get_data(
    req: HttpRequest,
    state: Data<AppState>,
) -> Result<HttpResponse, Error> {
    let cancel_token = CancellationToken::new();
    req.on_client_cancel(|| {  // <-- this api doesn't exist
      cancel_token.abort();
    });
    backend.query(..., cancel_token).await
}
Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
  • "I suspect the actual socket connection remains, just one specific fetch is aborted." This isn't really possible in HTTP 1. The only way to "abort" a request after its sent is to terminate the connection - there's no protocol for it. Don't know about HTTP 2 though. – Colonel Thirty Two Nov 22 '22 at 22:08

0 Answers0