fix(server): give a connection's writer its own task so a burst of requests cannot starve its replies
One task read and wrote a connection through a select. The reading branch dispatches a request and reads the next without yielding while frames are buffered, so the writing branch did not run until the burst ended. A room host that reconnects registers every room back to back, and each registration puts four events on its own queue of 64. Measured on a real Server with its own Native host: at 32 rooms the seventeenth registration found the queue full with about one event drained, the host was closed, and it reconnected into the same burst 57 times in a minute without one room coming back. At 16 rooms the queue peaked at exactly 64. The writer is now a task of its own, owned by the session through an abort-on-drop handle, which is how a WebSocket with an outbound queue is usually arranged. Nothing else about the session changes: the same queue, the same close signal, the same drain at close.
This commit is contained in:
1 parent
9e359a529d
commit
bb6eaf6433
2 files changed
+10
-6
No files matched your search
@@ -47,7 +47,7 @@ ring = "0.17"
|
||||
semver = "1"
|
||||
reqwest = { version = "=0.13.5", default-features = false, features = ["rustls-no-provider"] }
|
||||
webpki-roots = "1"
|
||||
tokio-util = { version = "0.7", features = ["io-util"] }
|
||||
tokio-util = { version = "0.7", features = ["io-util", "rt"] }
|
||||
tempfile = "=3.27.0"
|
||||
fs4 = { version = "=1.1.0", default-features = false }
|
||||
zip = { version = "=9.2.1", default-features = false, features = ["deflate-flate2-zlib-rs"] }
|
||||
|
||||
@@ -515,9 +515,13 @@ async fn session(mut socket: WebSocket, state: Domain, local_entry: bool) {
|
||||
}
|
||||
let (mut writer, mut reader) = socket.split();
|
||||
{
|
||||
// A blocked write must not stop reading HostOutput: the room host may
|
||||
// itself be waiting for us to drain its large response before reading.
|
||||
let writing = async {
|
||||
// Reading and writing progress independently, each in its own task and
|
||||
// both owned here. A blocked write must not stop reading HostOutput: the
|
||||
// room host may itself be waiting for us to drain its large response
|
||||
// before reading. And a run of buffered requests must not keep its own
|
||||
// replies from draining: in one task, a host registering seventeen rooms
|
||||
// filled its queue before the writer ran once, and was closed each time.
|
||||
let writing = tokio_util::task::AbortOnDropHandle::new(tokio::spawn(async move {
|
||||
let mut heartbeat = tokio::time::interval(Duration::from_secs(5));
|
||||
loop {
|
||||
tokio::select! {
|
||||
@@ -539,7 +543,7 @@ async fn session(mut socket: WebSocket, state: Domain, local_entry: bool) {
|
||||
}
|
||||
}
|
||||
send_frame(&mut writer, Message::Close(None)).await;
|
||||
};
|
||||
}));
|
||||
let reading = async {
|
||||
loop {
|
||||
let incoming = match tokio::time::timeout(Duration::from_secs(20), reader.next()).await {
|
||||
@@ -568,7 +572,7 @@ async fn session(mut socket: WebSocket, state: Domain, local_entry: bool) {
|
||||
_ = &mut writing => {},
|
||||
_ = &mut reading => {
|
||||
let _ = close_writer.send(true);
|
||||
writing.await;
|
||||
let _ = writing.await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user