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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions crates/selvaged/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions crates/selvaged/src/net/session.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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<u8>, shared: &Shared) {
async fn relay(&self, frame: Bytes, shared: &Shared) {
deliver(
shared,
&self.room_id,
Expand Down
15 changes: 9 additions & 6 deletions crates/selvaged/src/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -33,7 +34,7 @@ pub const MAX_QUEUE_BYTES: usize = 32 * 1024 * 1024;
#[derive(Debug, Clone)]
pub enum Outbound {
Text(String),
Binary(Vec<u8>),
Binary(Bytes),
Ping(Vec<u8>),
Close(u16, String),
}
Expand All @@ -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(),
}
}
Expand Down Expand Up @@ -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());
}
Expand All @@ -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()]
Expand Down Expand Up @@ -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"));
Expand All @@ -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);
Expand Down