Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 31 additions & 16 deletions crates/workshop/server/tests/it/realtime_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};

use axum::Router;
use axum::body::Bytes;
use axum::extract::State;
use axum::extract::ws::{CloseFrame, Message, WebSocketUpgrade};
use axum::http::{HeaderMap, StatusCode, Uri, header};
Expand Down Expand Up @@ -254,35 +255,49 @@ async fn upstream_close(ws: WebSocketUpgrade) -> Response {
})
}

/// The size of each frame the stalling Gateway sends. The relay reads the
/// Gateway under tungstenite's default 16 MiB `max_frame_size`, and refuses a
/// larger frame before it forwards anything, so no browser send would stall.
const STALL_FRAME_BYTES: usize = 8 * 1024 * 1024;

/// The most frames the stalling Gateway sends: more bytes than the socket
/// buffers between the relay and the browser hold, so a browser that never
/// reads stalls one of the relay's sends.
const STALL_FRAMES: usize = 16;

#[derive(Clone, Default)]
struct StalledPeerProbe {
frame_sent: Arc<AtomicBool>,
frame_sent_event: Arc<Notify>,
struct RelayReleaseProbe {
released: Arc<AtomicBool>,
released_event: Arc<Notify>,
}

impl StalledPeerProbe {
async fn wait_for_frame(&self) {
let notified = self.frame_sent_event.notified();
if self.frame_sent.load(Ordering::Acquire) {
impl RelayReleaseProbe {
async fn wait_for_release(&self) {
let notified = self.released_event.notified();
if self.released.load(Ordering::Acquire) {
return;
}
notified.await;
}
}

async fn send_large_frame_then_disconnect(
State(probe): State<StalledPeerProbe>,
/// Sends frames until the relay stops taking them, then reports when the
/// relay drops the Gateway socket, which it does only once it has given up on
/// the browser.
async fn send_frames_until_the_relay_lets_go(
State(probe): State<RelayReleaseProbe>,
ws: WebSocketUpgrade,
) -> Response {
ws.on_upgrade(move |mut socket| async move {
if socket
.send(Message::Binary(vec![0x5a; 32 * 1024 * 1024].into()))
.await
.is_ok()
{
probe.frame_sent.store(true, Ordering::Release);
probe.frame_sent_event.notify_one();
let frame = Bytes::from(vec![0x5a; STALL_FRAME_BYTES]);
for _ in 0..STALL_FRAMES {
if socket.send(Message::Binary(frame.clone())).await.is_err() {
break;
}
}
while let Some(Ok(_)) = socket.recv().await {}
probe.released.store(true, Ordering::Release);
probe.released_event.notify_one();
})
}

Expand Down
33 changes: 23 additions & 10 deletions crates/workshop/server/tests/it/realtime_relay/overload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
use super::*;

#[tokio::test]
async fn stalled_browser_cleanup_is_bounded_after_gateway_disconnect() {
let probe = StalledPeerProbe::default();
async fn a_stalled_browser_send_is_canceled_within_the_relay_deadline() {
let probe = RelayReleaseProbe::default();
let gateway = spawn_gateway(
Router::new()
.route("/v1/realtime", get(send_large_frame_then_disconnect))
.route("/v1/realtime", get(send_frames_until_the_relay_lets_go))
.with_state(probe.clone()),
)
.await;
Expand All @@ -16,15 +16,28 @@ async fn stalled_browser_cleanup_is_bounded_after_gateway_disconnect() {
tokio_tungstenite::connect_async(server.ws_url("/v1/realtime?intent=transcription"))
.await
.expect("the Workshop Realtime socket upgrades");
tokio::time::timeout(RECV_TIMEOUT, probe.wait_for_frame())
// The browser reads nothing until the relay lets go: a read would make
// room for the stalled send, which could then finish inside the deadline.
tokio::time::timeout(RECV_TIMEOUT, probe.wait_for_release())
.await
.expect("the Gateway fills the relay's browser send");
.expect("the relay gives up on a browser that never reads");

let first = tokio::time::timeout(RECV_TIMEOUT, socket.next())
.await
.expect("bounded relay cleanup releases the stalled browser");
// The frames the sockets took before the stall can arrive whole, so the
// test counts frames instead of checking the first one.
let mut frames = 0;
loop {
let next = tokio::time::timeout(RECV_TIMEOUT, socket.next())
.await
.expect("the released browser socket ends");
match next {
Some(Ok(ClientMessage::Binary(_))) => frames += 1,
Some(Ok(ClientMessage::Close(_)) | Err(_)) | None => break,
Some(Ok(_)) => {}
}
}
assert!(
!matches!(first, Some(Ok(ClientMessage::Binary(_)))),
"the stalled send is canceled before peer reads can release it"
frames < STALL_FRAMES,
"the stalled send is canceled before peer reads can release it: \
{frames} of {STALL_FRAMES} frames reached the browser"
);
}
Loading