fix(transport-sse-server): cleanup on connection drop (#165)
This commit is contained in:
parent
6de444c62f
commit
030b6f0858
1 changed files with 15 additions and 0 deletions
|
|
@ -91,6 +91,8 @@ async fn sse_handler(
|
||||||
use tokio_util::sync::PollSender;
|
use tokio_util::sync::PollSender;
|
||||||
let (from_client_tx, from_client_rx) = tokio::sync::mpsc::channel(64);
|
let (from_client_tx, from_client_rx) = tokio::sync::mpsc::channel(64);
|
||||||
let (to_client_tx, to_client_rx) = tokio::sync::mpsc::channel(64);
|
let (to_client_tx, to_client_rx) = tokio::sync::mpsc::channel(64);
|
||||||
|
let to_client_tx_clone = to_client_tx.clone();
|
||||||
|
|
||||||
app.txs
|
app.txs
|
||||||
.write()
|
.write()
|
||||||
.await
|
.await
|
||||||
|
|
@ -125,6 +127,19 @@ async fn sse_handler(
|
||||||
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)),
|
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)),
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
tokio::spawn(async move {
|
||||||
|
// Wait for connection closure
|
||||||
|
to_client_tx_clone.closed().await;
|
||||||
|
|
||||||
|
// Clean up session
|
||||||
|
let session_id = session.clone();
|
||||||
|
let tx_store = app.txs.clone();
|
||||||
|
let mut txs = tx_store.write().await;
|
||||||
|
txs.remove(&session_id);
|
||||||
|
tracing::debug!(%session_id, "Closed session and cleaned up resources");
|
||||||
|
});
|
||||||
|
|
||||||
Ok(Sse::new(stream).keep_alive(KeepAlive::new().interval(ping_interval)))
|
Ok(Sse::new(stream).keep_alive(KeepAlive::new().interval(ping_interval)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue