diff --git a/Cargo.lock b/Cargo.lock index 59ace10..4e14d59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -727,6 +727,7 @@ dependencies = [ name = "selvaged" version = "0.5.13" dependencies = [ + "bytes", "futures-util", "getrandom 0.4.3", "selvage-protocol", diff --git a/Cargo.toml b/Cargo.toml index cfa1379..40723b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -73,6 +73,7 @@ selvage-client = { path = "crates/client" } selvaged = { path = "crates/selvaged" } selvage-harness = { path = "crates/harness" } +bytes = "1" futures-util = { version = "0.3", default-features = false, features = ["sink"] } serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/crates/selvaged/Cargo.toml b/crates/selvaged/Cargo.toml index 2587266..168b87d 100644 --- a/crates/selvaged/Cargo.toml +++ b/crates/selvaged/Cargo.toml @@ -15,6 +15,7 @@ path = "src/main.rs" [dependencies] selvage-protocol.workspace = true +bytes.workspace = true tokio.workspace = true tokio-tungstenite.workspace = true futures-util.workspace = true diff --git a/crates/selvaged/src/net/session.rs b/crates/selvaged/src/net/session.rs index 28a4802..0950ad9 100644 --- a/crates/selvaged/src/net/session.rs +++ b/crates/selvaged/src/net/session.rs @@ -1,6 +1,7 @@ //! The session protocol: the handshake, seating a connection, and the session methods //! a seated connection answers. +use bytes::Bytes; use std::sync::atomic::{AtomicBool, Ordering}; use futures_util::StreamExt; @@ -659,7 +660,9 @@ impl Session { ) -> bool { match incoming { Some(Ok(Message::Binary(frame))) => { - self.relay(frame.to_vec(), shared).await; + // tungstenite slices a larger read buffer into `Bytes`. An exact-size + // copy keeps a queued small frame from holding that whole buffer. + self.relay(Bytes::copy_from_slice(&frame), shared).await; } Some(Ok(Message::Text(text))) => { self.handle_text(&text, shared).await; @@ -671,7 +674,7 @@ impl Session { } /// Relays a document or awareness payload to the rest of the room, untouched. - async fn relay(&self, frame: Vec, shared: &Shared) { + async fn relay(&self, frame: Bytes, shared: &Shared) { deliver( shared, &self.room_id, diff --git a/crates/selvaged/src/room.rs b/crates/selvaged/src/room.rs index 4b8e830..15ae09d 100644 --- a/crates/selvaged/src/room.rs +++ b/crates/selvaged/src/room.rs @@ -7,6 +7,7 @@ use std::collections::HashMap; use std::sync::Arc; use std::sync::atomic::{AtomicUsize, Ordering}; +use bytes::Bytes; use selvage_protocol::Keepalive; use selvage_protocol::PeerInfo; use tokio::sync::mpsc::{Receiver, Sender, channel}; @@ -33,7 +34,7 @@ pub const MAX_QUEUE_BYTES: usize = 32 * 1024 * 1024; #[derive(Debug, Clone)] pub enum Outbound { Text(String), - Binary(Vec), + Binary(Bytes), Ping(Vec), Close(u16, String), } @@ -46,7 +47,8 @@ impl Outbound { pub const fn payload_len(&self) -> usize { match self { Self::Text(text) => text.len(), - Self::Binary(bytes) | Self::Ping(bytes) => bytes.len(), + Self::Binary(bytes) => bytes.len(), + Self::Ping(bytes) => bytes.len(), Self::Close(_, reason) => reason.len(), } } @@ -510,7 +512,8 @@ mod tests { Some("r-1") ); let room = registry.room("r-1").expect("the room is minted"); - let chunk = Outbound::Binary(vec![0xA5u8; 8 * 1024 * 1024]); + let chunk = + Outbound::Binary(Bytes::from(vec![0xA5u8; 8 * 1024 * 1024])); for _ in 0..4 { assert!(send_all(&room.queues(None), &chunk).is_empty()); } @@ -520,7 +523,7 @@ mod tests { .map(|peer| peer.queue.queued_bytes()), Some(MAX_QUEUE_BYTES) ); - let over = Outbound::Binary(vec![0xA5u8; 1]); + let over = Outbound::Binary(Bytes::from(vec![0xA5u8; 1])); assert_eq!( send_all(&room.queues(None), &over), vec!["p-slow".to_string()] @@ -635,7 +638,7 @@ mod tests { /// empty guess are all refused, and only the room's own encoding matches. #[test] fn a_token_matches_only_whole() { - let token = "0123456789abcdef0123456789abcdef"; + let token = "0123456789abcdef0123456789abcdef"; // pragma: allowlist secret assert!(token_matches(token, token)); assert!(!token_matches(token, "0123456789abcdef0123456789abcde")); assert!(!token_matches(token, "0123456789abcdef0123456789abcdef0")); @@ -651,7 +654,7 @@ mod tests { /// no token at all — and seats the room's own token. #[test] fn admission_refuses_a_token_of_any_wrong_shape() { - let token = "0123456789abcdef0123456789abcdef"; + let token = "0123456789abcdef0123456789abcdef"; // pragma: allowlist secret let mut registry = Registry::default(); let (host, _leftovers) = peer_channel(peer("p-host", "Host"), MAX_QUEUE_BYTES);