Skip to content

client-api: Send WebSocket messages fragmented #2931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
45 changes: 44 additions & 1 deletion crates/client-api/src/routes/subscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ use tokio::sync::{mpsc, watch};
use tokio::task::JoinHandle;
use tokio::time::error::Elapsed;
use tokio::time::{sleep_until, timeout};
use tokio_tungstenite::tungstenite::protocol::frame::coding::{Data, OpCode};
use tokio_tungstenite::tungstenite::protocol::frame::Frame;
use tokio_tungstenite::tungstenite::Utf8Bytes;

use crate::auth::SpacetimeAuth;
Expand Down Expand Up @@ -1046,7 +1048,48 @@ async fn send_message<S: Sink<WsMessage> + Unpin>(
report_ws_sent_metrics(database_identity, workload, num_rows, timing, &msg_data);

let res = async {
ws.feed(datamsg_to_wsmsg(msg_data)).await?;
// EXPERIMENT: Send fragmented messages (RFC 6455, Section 5.4).
let (data, ty) = match datamsg_to_wsmsg(msg_data) {
WsMessage::Text(text) => (text.into(), Data::Text),
WsMessage::Binary(bin) => (bin, Data::Binary),
_ => unreachable!(),
};

const FRAGMENT_SIZE: usize = 4096;

let total_len = data.len();

let mut frames = Vec::with_capacity((total_len / FRAGMENT_SIZE) + 1);
let mut offset = 0;
while offset < total_len {
let end = (offset + FRAGMENT_SIZE).min(total_len);
let chunk = data.slice(offset..end);
frames.push(Frame::message(chunk, OpCode::Data(Data::Continue), false));
offset = end;
}

match frames.as_mut_slice() {
[] => {}
[single] => {
let hdr = single.header_mut();
hdr.is_final = true;
hdr.opcode = OpCode::Data(ty);
}
[first, .., last] => {
let hdr = first.header_mut();
hdr.is_final = false;
hdr.opcode = OpCode::Data(ty);

let hdr = last.header_mut();
hdr.is_final = true;
hdr.opcode = OpCode::Data(Data::Continue);
}
}

log::trace!("sending message in {} frames", frames.len());
for frame in frames {
ws.feed(WsMessage::Frame(frame)).await?;
}
// To reclaim the `msg_alloc` memory, we need `SplitSink` to push down
// its item slot to the inner sink, which will copy the `Bytes` and
// drop the reference.
Expand Down
Loading