Skip to content

Commit 3c661fb

Browse files
tlongwell-blocknpub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757Tyler Longwell
authored
Serialize fan-out EVENT frames once (#1459)
Signed-off-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Signed-off-by: Tyler Longwell <[email protected]> Co-authored-by: npub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757 <5217c5c2f7bfb4333e46d17c98a9255a52dadee18dcd43a43536b95e6776dfa0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <[email protected]>
1 parent d9c4e4a commit 3c661fb

2 files changed

Lines changed: 142 additions & 33 deletions

File tree

‎crates/buzz-relay/src/handlers/event.rs‎

Lines changed: 125 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
//! EVENT handler — WS dispatcher → ingest pipeline → fan-out.
22
3-
use std::sync::Arc;
3+
use std::{collections::HashMap, sync::Arc};
44

5+
use axum::body::Bytes;
56
use tracing::{debug, error, info, warn};
67

78
use buzz_core::event::StoredEvent;
@@ -50,6 +51,50 @@ fn bounded_kind_label(kind: u32) -> String {
5051
}
5152
}
5253

54+
fn event_frame_for_sub(sub_id: &str, event_json: &str) -> String {
55+
format!(r#"["EVENT","{}",{}]"#, sub_id, event_json)
56+
}
57+
58+
fn event_frame_bytes_for_sub(sub_id: &str, event_json: &str) -> Arc<Bytes> {
59+
Arc::new(Bytes::from(event_frame_for_sub(sub_id, event_json)))
60+
}
61+
62+
fn fanout_frame_cache<'a, I>(sub_ids: I, event_json: &str) -> HashMap<&'a str, Arc<Bytes>>
63+
where
64+
I: IntoIterator<Item = &'a str>,
65+
{
66+
let mut frames = HashMap::new();
67+
for sub_id in sub_ids {
68+
frames
69+
.entry(sub_id)
70+
.or_insert_with(|| event_frame_bytes_for_sub(sub_id, event_json));
71+
}
72+
frames
73+
}
74+
75+
fn send_fanout_frames<'a, I>(
76+
state: &AppState,
77+
recipients: I,
78+
frames: &HashMap<&'a str, Arc<Bytes>>,
79+
) -> u32
80+
where
81+
I: IntoIterator<Item = (crate::subscription::ConnId, &'a str)>,
82+
{
83+
let mut drop_count = 0u32;
84+
for (conn_id, sub_id) in recipients {
85+
let frame = frames
86+
.get(sub_id)
87+
.expect("fan-out frame cache covers every recipient subscription id");
88+
if !state
89+
.conn_manager
90+
.send_to_text_bytes(conn_id, Arc::clone(frame))
91+
{
92+
drop_count += 1;
93+
}
94+
}
95+
drop_count
96+
}
97+
5398
/// Drop recipients without access before fan-out on a private channel.
5499
///
55100
/// Open and channel-less events skip membership filtering (open channel-scoped
@@ -188,13 +233,17 @@ pub(crate) async fn fan_out_event_to_local_subscribers(
188233
return;
189234
}
190235
};
191-
let mut drop_count = 0u32;
192-
for (conn_id, sub_id) in &matches {
193-
let msg = format!(r#"["EVENT","{}",{}]"#, sub_id, event_json);
194-
if !state.conn_manager.send_to(*conn_id, msg) {
195-
drop_count += 1;
196-
}
197-
}
236+
let frames = fanout_frame_cache(
237+
matches.iter().map(|(_, sub_id)| sub_id.as_str()),
238+
&event_json,
239+
);
240+
let drop_count = send_fanout_frames(
241+
state,
242+
matches
243+
.iter()
244+
.map(|(conn_id, sub_id)| (*conn_id, sub_id.as_str())),
245+
&frames,
246+
);
198247
if drop_count > 0 {
199248
tracing::warn!(
200249
event_id = %stored.event.id.to_hex(),
@@ -244,13 +293,17 @@ pub async fn fan_out_pubsub_event(state: &Arc<AppState>, channel_event: buzz_pub
244293
return;
245294
}
246295
};
247-
let mut drop_count = 0u32;
248-
for (conn_id, sub_id) in &matches {
249-
let msg = format!(r#"["EVENT","{}",{}]"#, sub_id, event_json);
250-
if !state.conn_manager.send_to(*conn_id, msg) {
251-
drop_count += 1;
252-
}
253-
}
296+
let frames = fanout_frame_cache(
297+
matches.iter().map(|(_, sub_id)| sub_id.as_str()),
298+
&event_json,
299+
);
300+
let drop_count = send_fanout_frames(
301+
state,
302+
matches
303+
.iter()
304+
.map(|(conn_id, sub_id)| (*conn_id, sub_id.as_str())),
305+
&frames,
306+
);
254307
if drop_count > 0 {
255308
tracing::warn!(
256309
event_id = %stored.event.id.to_hex(),
@@ -391,23 +444,26 @@ async fn dispatch_persistent_event_inner(
391444
})
392445
.flatten();
393446
// Author-only delivery gating (NIP-ER reminders) is enforced centrally in
394-
// filter_fanout_by_access, applied to `matches` above before this loop.
395-
let mut drop_count = 0u32;
396-
for (target_conn_id, sub_id) in &matches {
397-
if let Some(ref owner_hex) = dm_visibility_owner {
398-
let is_owner = state
399-
.conn_manager
400-
.pubkey_for(*target_conn_id)
401-
.is_some_and(|pk| hex::encode(pk) == *owner_hex);
402-
if !is_owner {
403-
continue;
447+
// filter_fanout_by_access, applied to `matches` above before this loop. The
448+
// DM visibility owner gate is an additional delivery fence, so build shared
449+
// frames only after applying it to the already access-filtered recipient set.
450+
let recipients: Vec<_> = matches
451+
.iter()
452+
.filter_map(|(target_conn_id, sub_id)| {
453+
if let Some(ref owner_hex) = dm_visibility_owner {
454+
let is_owner = state
455+
.conn_manager
456+
.pubkey_for(*target_conn_id)
457+
.is_some_and(|pk| hex::encode(pk) == *owner_hex);
458+
if !is_owner {
459+
return None;
460+
}
404461
}
405-
}
406-
let msg = format!(r#"["EVENT","{}",{}]"#, sub_id, event_json);
407-
if !state.conn_manager.send_to(*target_conn_id, msg) {
408-
drop_count += 1;
409-
}
410-
}
462+
Some((*target_conn_id, sub_id.as_str()))
463+
})
464+
.collect();
465+
let frames = fanout_frame_cache(recipients.iter().map(|(_, sub_id)| *sub_id), &event_json);
466+
let drop_count = send_fanout_frames(state, recipients, &frames);
411467
if drop_count > 0 {
412468
tracing::warn!(
413469
event_id = %event_id_hex,
@@ -1094,6 +1150,8 @@ fn single_tag_content<'a>(event: &'a Event, tag_name: &str) -> Result<&'a str, S
10941150

10951151
#[cfg(test)]
10961152
mod tests {
1153+
use std::sync::Arc;
1154+
10971155
use buzz_core::kind::{
10981156
KIND_AGENT_OBSERVER_FRAME, KIND_CANVAS, KIND_FORUM_COMMENT, KIND_FORUM_POST,
10991157
KIND_FORUM_VOTE, KIND_PRESENCE_UPDATE, KIND_STREAM_MESSAGE, KIND_STREAM_MESSAGE_DIFF,
@@ -1104,6 +1162,42 @@ mod tests {
11041162
};
11051163
use nostr::{EventBuilder, Keys, Kind, Tag};
11061164

1165+
#[test]
1166+
fn fanout_event_frame_matches_legacy_format_byte_for_byte() {
1167+
let sub_id = "sub-id";
1168+
let event_json = r#"{"id":"abc","tags":[["p","target"]],"content":"hello"}"#;
1169+
let expected = format!(r#"["EVENT","{}",{}]"#, sub_id, event_json);
1170+
1171+
assert_eq!(super::event_frame_for_sub(sub_id, event_json), expected);
1172+
assert_eq!(
1173+
super::event_frame_bytes_for_sub(sub_id, event_json)
1174+
.as_ref()
1175+
.as_ref(),
1176+
expected.as_bytes()
1177+
);
1178+
}
1179+
1180+
#[test]
1181+
fn fanout_frame_cache_reuses_frames_within_one_cycle_only() {
1182+
let event_json = r#"{"id":"abc"}"#;
1183+
let frames = super::fanout_frame_cache(["same", "other", "same"], event_json);
1184+
1185+
assert_eq!(frames.len(), 2, "duplicate sub ids share one cached frame");
1186+
assert_eq!(
1187+
frames.get("same").expect("same frame").as_ref().as_ref(),
1188+
format!(r#"["EVENT","same",{}]"#, event_json).as_bytes()
1189+
);
1190+
1191+
let next_cycle = super::fanout_frame_cache(["same"], event_json);
1192+
assert!(
1193+
!Arc::ptr_eq(
1194+
frames.get("same").expect("same frame"),
1195+
next_cycle.get("same").expect("same frame in next cycle")
1196+
),
1197+
"fan-out frame sharing must not escape a single cycle"
1198+
);
1199+
}
1200+
11071201
#[test]
11081202
fn channel_scoped_content_kinds_require_h_tags() {
11091203
for kind in [

‎crates/buzz-relay/src/state.rs‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
44
use std::sync::Arc;
55
use std::time::Instant;
66

7-
use axum::extract::ws::Message as WsMessage;
7+
use axum::body::Bytes;
8+
use axum::extract::ws::{Message as WsMessage, Utf8Bytes as WsUtf8Bytes};
89
use dashmap::DashMap;
910
use tokio::sync::mpsc;
1011
use tokio::sync::Semaphore;
@@ -155,9 +156,23 @@ impl ConnectionManager {
155156
/// On sustained backpressure (>grace_limit consecutive full buffers),
156157
/// cancels the connection. Transient stalls get a warning only.
157158
pub fn send_to(&self, conn_id: Uuid, msg: String) -> bool {
159+
self.try_send_ws_message(conn_id, WsMessage::Text(msg.into()))
160+
}
161+
162+
/// Sends an already-serialized UTF-8 text payload to the given connection.
163+
///
164+
/// The shared `Bytes` payload is cloned into the outbound WS message without
165+
/// copying the frame body. Callers must only pass valid UTF-8 bytes.
166+
pub fn send_to_text_bytes(&self, conn_id: Uuid, msg: Arc<Bytes>) -> bool {
167+
let text = WsUtf8Bytes::try_from(Bytes::clone(msg.as_ref()))
168+
.expect("relay fan-out frames are serialized UTF-8 JSON");
169+
self.try_send_ws_message(conn_id, WsMessage::Text(text))
170+
}
171+
172+
fn try_send_ws_message(&self, conn_id: Uuid, msg: WsMessage) -> bool {
158173
if let Some(entry) = self.connections.get(&conn_id) {
159174
let conn = entry.value();
160-
match conn.tx.try_send(WsMessage::Text(msg.into())) {
175+
match conn.tx.try_send(msg) {
161176
Ok(_) => {
162177
conn.backpressure_count.store(0, Ordering::Relaxed);
163178
true

0 commit comments

Comments
 (0)