Skip to content

Commit 7bd3760

Browse files
tlongwell-blocknpub12gtutshhh76rx0jx697f32f9tffd4hhp3hx58fp4x6u4uemkm7sqf8f757npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dTyler Longwell
authored
perf(relay): defer post-commit dispatch and avoid verify clone (#1453)
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: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <[email protected]>
1 parent 88c089d commit 7bd3760

2 files changed

Lines changed: 125 additions & 34 deletions

File tree

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

Lines changed: 117 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -241,13 +241,67 @@ pub async fn fan_out_pubsub_event(state: &Arc<AppState>, channel_event: buzz_pub
241241
}
242242
}
243243

244-
/// Publish a stored event to subscribers and kick off async side effects.
244+
/// Schedule post-commit delivery/side effects for a stored event.
245+
///
246+
/// This intentionally returns after only the bounded audit enqueue has completed:
247+
/// NIP-01 `OK` means the event was durably accepted, not that Redis publish,
248+
/// local fan-out, or workflow triggering have completed. Keeping audit enqueue on
249+
/// the awaited path preserves the bounded-channel backpressure posture when the
250+
/// audit DB is overloaded; the spawned task still runs the same guarded fan-out
251+
/// path, Redis publish, `mark_local_event` echo dedupe, and delivery metrics as
252+
/// the former inline path.
245253
pub(crate) async fn dispatch_persistent_event(
246254
tenant: &TenantContext,
247255
state: &Arc<AppState>,
248256
stored_event: &StoredEvent,
249257
kind_u32: u32,
250258
actor_pubkey_hex: &str,
259+
) -> usize {
260+
let event_id_hex = stored_event.event.id.to_hex();
261+
enqueue_event_created_audit(
262+
tenant,
263+
state,
264+
stored_event,
265+
kind_u32,
266+
actor_pubkey_hex,
267+
&event_id_hex,
268+
)
269+
.await;
270+
271+
let tenant = tenant.clone();
272+
let state = Arc::clone(state);
273+
let stored_event = stored_event.clone();
274+
let actor_pubkey_hex = actor_pubkey_hex.to_owned();
275+
276+
metrics::counter!("buzz_post_commit_dispatch_scheduled_total").increment(1);
277+
tokio::spawn(async move {
278+
let recipients = dispatch_persistent_event_inner(
279+
&tenant,
280+
&state,
281+
&stored_event,
282+
kind_u32,
283+
&actor_pubkey_hex,
284+
false,
285+
)
286+
.await;
287+
debug!(
288+
event_id = %event_id_hex,
289+
recipients,
290+
"post-commit dispatch complete"
291+
);
292+
});
293+
294+
0
295+
}
296+
297+
/// Run post-commit delivery/side effects for a stored event.
298+
async fn dispatch_persistent_event_inner(
299+
tenant: &TenantContext,
300+
state: &Arc<AppState>,
301+
stored_event: &StoredEvent,
302+
kind_u32: u32,
303+
actor_pubkey_hex: &str,
304+
enqueue_audit: bool,
251305
) -> usize {
252306
// No `crate::conformance` emit here — the spec doesn't have a
253307
// separate fan-out action. Acceptance was already recorded at the
@@ -285,8 +339,15 @@ pub(crate) async fn dispatch_persistent_event(
285339
"Fan-out"
286340
);
287341

288-
let event_json = serde_json::to_string(&stored_event.event)
289-
.expect("nostr::Event serialization is infallible for well-formed events");
342+
let event_json = match serde_json::to_string(&stored_event.event) {
343+
Ok(json) => json,
344+
Err(e) => {
345+
error!(event_id = %event_id_hex, "Failed to serialize event for fan-out: {e}");
346+
metrics::counter!("buzz_post_commit_dispatch_errors_total", "stage" => "serialize")
347+
.increment(1);
348+
return 0;
349+
}
350+
};
290351
// For viewer-private snapshots (kind:30622), live fan-out must reach only the
291352
// owner — a kindless `ids:[…]` subscription can otherwise match it. Pull paths
292353
// (HTTP /query, WS historical) are gated separately by reader_authorized_for_event.
@@ -332,32 +393,16 @@ pub(crate) async fn dispatch_persistent_event(
332393
// out-of-band index to feed. The old Typesense `index_event` worker and its
333394
// `search_index_tx` mpsc are gone with the Typesense backend.
334395

335-
// Audit via bounded channel (capacity 1000). Uses .send().await so entries
336-
// are never silently dropped — backpressure propagates to the event handler
337-
// if the queue is full. This is intentional: the audit advisory lock already
338-
// serializes writes (at most 1 in-flight), so a full queue means the audit
339-
// DB is genuinely overloaded and the relay should slow down rather than
340-
// accumulate unbounded in-memory state. DB write failures in the worker are
341-
// logged but not retried (same as the previous per-event tokio::spawn).
342-
let audit_entry = buzz_audit::NewAuditEntry {
343-
community_id: tenant.community(),
344-
action: buzz_audit::AuditAction::EventCreated,
345-
// Record the *actor* the caller resolved (authenticated principal for
346-
// ingest, triggering user for workflow posts), not `stored_event.event
347-
// .pubkey`. For relay-signed events (workflow sink, side-effect emits)
348-
// the claimed author is the relay key, so deriving from the event would
349-
// erase the human behind the action from the audit trail. This mirrors
350-
// the pre-rewrite semantics, ported to the raw-bytes column.
351-
actor_pubkey: hex::decode(actor_pubkey_hex).ok(),
352-
object_id: Some(event_id_hex.clone()),
353-
detail: serde_json::json!({
354-
"event_kind": kind_u32,
355-
"channel_id": stored_event.channel_id,
356-
}),
357-
};
358-
if let Err(e) = state.audit_tx.send(audit_entry).await {
359-
error!(event_id = %event_id_hex, "Audit channel closed — entry lost: {e}");
360-
metrics::counter!("buzz_audit_send_errors_total").increment(1);
396+
if enqueue_audit {
397+
enqueue_event_created_audit(
398+
tenant,
399+
state,
400+
stored_event,
401+
kind_u32,
402+
actor_pubkey_hex,
403+
&event_id_hex,
404+
)
405+
.await;
361406
}
362407

363408
// Skip workflow triggering for workflow-execution kinds and relay-signed workflow messages.
@@ -398,6 +443,43 @@ pub(crate) async fn dispatch_persistent_event(
398443
matches.len()
399444
}
400445

446+
async fn enqueue_event_created_audit(
447+
tenant: &TenantContext,
448+
state: &Arc<AppState>,
449+
stored_event: &StoredEvent,
450+
kind_u32: u32,
451+
actor_pubkey_hex: &str,
452+
event_id_hex: &str,
453+
) {
454+
// Audit via bounded channel (capacity 1000). Uses .send().await so entries
455+
// are never silently dropped — backpressure propagates to the event handler
456+
// if the queue is full. This is intentional: the audit advisory lock already
457+
// serializes writes (at most 1 in-flight), so a full queue means the audit
458+
// DB is genuinely overloaded and the relay should slow down rather than
459+
// accumulate unbounded in-memory state. DB write failures in the worker are
460+
// logged but not retried (same as the previous per-event tokio::spawn).
461+
let audit_entry = buzz_audit::NewAuditEntry {
462+
community_id: tenant.community(),
463+
action: buzz_audit::AuditAction::EventCreated,
464+
// Record the *actor* the caller resolved (authenticated principal for
465+
// ingest, triggering user for workflow posts), not `stored_event.event
466+
// .pubkey`. For relay-signed events (workflow sink, side-effect emits)
467+
// the claimed author is the relay key, so deriving from the event would
468+
// erase the human behind the action from the audit trail. This mirrors
469+
// the pre-rewrite semantics, ported to the raw-bytes column.
470+
actor_pubkey: hex::decode(actor_pubkey_hex).ok(),
471+
object_id: Some(event_id_hex.to_owned()),
472+
detail: serde_json::json!({
473+
"event_kind": kind_u32,
474+
"channel_id": stored_event.channel_id,
475+
}),
476+
};
477+
if let Err(e) = state.audit_tx.send(audit_entry).await {
478+
error!(event_id = %event_id_hex, "Audit channel closed — entry lost: {e}");
479+
metrics::counter!("buzz_audit_send_errors_total").increment(1);
480+
}
481+
}
482+
401483
/// Handle an EVENT message from a WebSocket connection.
402484
///
403485
/// Extracts auth from the WS connection, dispatches ephemeral events locally,
@@ -1459,12 +1541,13 @@ mod tests {
14591541
let event_id_hex = event.id.to_hex();
14601542
let stored = StoredEvent::new(event, None);
14611543

1462-
super::super::dispatch_persistent_event(
1544+
super::super::dispatch_persistent_event_inner(
14631545
&tenant,
14641546
&state,
14651547
&stored,
14661548
KIND_PRESENCE_UPDATE,
14671549
&actor_hex,
1550+
true,
14681551
)
14691552
.await;
14701553

@@ -1559,20 +1642,22 @@ mod tests {
15591642
"test precondition: the two events must have distinct ids"
15601643
);
15611644

1562-
super::super::dispatch_persistent_event(
1645+
super::super::dispatch_persistent_event_inner(
15631646
&ta,
15641647
&state,
15651648
&a_stored,
15661649
KIND_PRESENCE_UPDATE,
15671650
&actor_hex,
1651+
true,
15681652
)
15691653
.await;
1570-
super::super::dispatch_persistent_event(
1654+
super::super::dispatch_persistent_event_inner(
15711655
&tb,
15721656
&state,
15731657
&b_stored,
15741658
KIND_PRESENCE_UPDATE,
15751659
&actor_hex,
1660+
true,
15761661
)
15771662
.await;
15781663

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,8 +1244,13 @@ async fn ingest_event_inner(
12441244
return Err(IngestError::Rejected("restricted: relay-only kind".into()));
12451245
}
12461246

1247-
let event_clone = event.clone();
1248-
let verify_result = tokio::task::spawn_blocking(move || verify_event(&event_clone)).await;
1247+
// Share the event with the verify task via Arc instead of deep-cloning it
1248+
// (tags + up to 256 KB of content). spawn_blocking only needs 'static, not
1249+
// ownership; once it completes its Arc is dropped, so try_unwrap returns
1250+
// the original event without ever having copied it.
1251+
let event = std::sync::Arc::new(event);
1252+
let event_for_verify = std::sync::Arc::clone(&event);
1253+
let verify_result = tokio::task::spawn_blocking(move || verify_event(&event_for_verify)).await;
12491254
match verify_result {
12501255
Ok(Ok(())) => {}
12511256
Ok(Err(e)) => {
@@ -1258,6 +1263,7 @@ async fn ingest_event_inner(
12581263
));
12591264
}
12601265
}
1266+
let event = std::sync::Arc::try_unwrap(event).unwrap_or_else(|arc| (*arc).clone());
12611267

12621268
const MAX_TIMESTAMP_DRIFT_SECS: i64 = 900; // ±15 minutes
12631269
let now = chrono::Utc::now().timestamp();

0 commit comments

Comments
 (0)