Skip to content

Commit 326e230

Browse files
baxenHoneyclaude
authored
perf(relay): drop per-pulse ban reads and owner-link writes on HTTP (#8233)
## Why Agents send an HTTP typing pulse (`POST /events`, kind 20002) every 3–5 seconds while they work. #8207 already routes the pulse's write-restriction and serving checks through #8208's short-TTL caches. Two per-pulse costs were still left at the membership step: - **A fresh ban read** for the agent, plus its owner when the agent comes in through NIP-OA (`deny_banned` → `community_ban_outcome`). - **Four writer round-trips for owner-admitted agents**, on every request: `materialize_nip_oa_owner` runs 2× `ensure_user` + the `agent_owner_pubkey` upsert + an `is_agent_owner` confirm, even though the link already exists. ## What changes - **Typing's ban check reads the restriction cache.** `submit_event` already parses the event before the membership step, so typing pulses call a new `enforce_relay_membership_ephemeral`. Its ban check reads through `restriction_state_cached`, the same 30s bound the pulse's write-restriction check already has. Every other HTTP route, persistent `POST /events`, and socket auth still read fresh (`cached: false`). - **`materialize_nip_oa_owner` skips a link it already knows.** If `observer_owner_cache` already holds `(community, agent, owner) = true`, it returns `true` right away. `agent_owner_pubkey` has one production writer, which sets it only `WHERE agent_owner_pubkey IS NULL`, and nothing clears it, so the skipped writes were no-ops. This helps every caller (HTTP, socket auth, audio). The owner-link disconnect only runs on the first write, so it still runs. - **`observer_owner_cache` capacity goes from 1k to 100k**, so concurrently active agents actually hit it. That's tens of MB per pod at most, the same scale as `restriction_cache`. Per-pulse Postgres work at the membership step: | Agent | Before | After | |---|---|---| | Direct relay member | host lookup (writer) + membership + ban | host lookup (writer) + membership | | Owner-admitted (NIP-OA) | host lookup + 2 memberships + 2 bans + 4 writer trips | host lookup + 2 memberships | ## Not in this PR - **Caching host → community.** It's the tenant-binding step every HTTP request goes through, and it reads from the writer. Community deletion depends on new requests not binding once a community leaves `active`. A cache would keep binding a quiescing community for its TTL. The serving-write leases may make that harmless, but that needs a closer look than a "safe fixes" PR. - **Caching relay membership.** Removing a member has no HTTP equivalent of the socket disconnect, so a cache would delay removal by its TTL. - **The desktop client reusing one connection.** That's going in a separate buzz-app PR. ## Unrelated one-liner The `sadscan` pre-commit hook scans whole staged files. It blocked this commit on a pre-existing test-only URI in `handlers/auth.rs` (`postgres://buzz:[email protected]:1/buzz`, a deliberately unreachable stub). I added the hook's own `// sadscan:disable np.postgres.1` marker to that line. ## Testing - New Postgres tests. I confirmed each one fails when its guard is removed: - `typing_ban_gate_uses_cached_restriction_row`: a member banned in Postgres with a cached clear row can still type. Fails if typing reads fresh. - `persistent_post_ban_gate_reads_fresh`: a stale cached ban doesn't refuse a kind-9 post that Postgres allows. Fails if every kind reads the cache. - `known_owner_link_skips_materialize_writes`: after the link is cached, deleting the agent row behind the cache's back stays deleted. Fails without the short-circuit. - `scripts/postgres-test-run.sh -p buzz-relay --lib --tests`: 397/397 passed. This was local PG14. - `cargo test -p buzz-relay`: 1430 passed and 7 failed. The same 7 fail without this commit in this environment: 6 `api::media` tests can't find a `buzz` role on :5432, and `mesh_demo` returns a 504. - `cargo clippy -p buzz-relay --all-targets -D warnings` is clean. - An independent agent review found no blockers. Its optional suggestions are test gaps: nothing pins the owner-cascade cached read, or the `false` literals at socket auth. - **Not yet done:** a run against a live local relay, and human testing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Honey <8e307ae0076a4dab6b94b036ea3edc7e08f823a625269c1e6919e881a048b4d2@buzz.block.builderlab.xyz> Co-authored-by: Honey <8e307ae0076a4dab6b94b036ea3edc7e08f823a625269c1e6919e881a048b4d2@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 19aa619 commit 326e230

5 files changed

Lines changed: 213 additions & 29 deletions

File tree

‎crates/buzz-relay/src/api/bridge.rs‎

Lines changed: 73 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,17 +1118,33 @@ async fn submit_event_authed(
11181118
}
11191119
};
11201120

1121+
let kind_u32 = buzz_core::kind::event_kind_u32(&event);
1122+
let is_typing = kind_u32 == buzz_core::kind::KIND_TYPING_INDICATOR;
1123+
11211124
// Enforce relay membership (with NIP-OA fallback via x-auth-tag header).
1125+
// Typing pulses arrive every few seconds per working agent, so their ban
1126+
// check uses the same short-TTL cache as their write-restriction check.
11221127
let auth_tag = super::relay_members::extract_auth_tag_header(headers);
1123-
let nip_oa_owner = match super::relay_members::enforce_relay_membership(
1124-
state,
1125-
tenant.community(),
1126-
&pubkey_bytes,
1127-
auth_tag,
1128-
signed_auth_created_at,
1129-
)
1130-
.await
1131-
{
1128+
let membership = if is_typing {
1129+
super::relay_members::enforce_relay_membership_ephemeral(
1130+
state,
1131+
tenant.community(),
1132+
&pubkey_bytes,
1133+
auth_tag,
1134+
signed_auth_created_at,
1135+
)
1136+
.await
1137+
} else {
1138+
super::relay_members::enforce_relay_membership(
1139+
state,
1140+
tenant.community(),
1141+
&pubkey_bytes,
1142+
auth_tag,
1143+
signed_auth_created_at,
1144+
)
1145+
.await
1146+
};
1147+
let nip_oa_owner = match membership {
11321148
Ok(owner) => owner.or_else(|| {
11331149
if !state.config.require_relay_membership {
11341150
super::relay_members::extract_nip_oa_owner(
@@ -1151,15 +1167,14 @@ async fn submit_event_authed(
11511167
super::relay_members::materialize_nip_oa_owner(state, tenant, &pubkey, &owner).await;
11521168
}
11531169

1154-
let kind_u32 = buzz_core::kind::event_kind_u32(&event);
11551170
let auth = IngestAuth::Http {
11561171
pubkey,
11571172
scopes: buzz_auth::Scope::all_known(), // Pure Nostr: full scopes, channel access via membership
11581173
auth_method: crate::handlers::ingest::HttpAuthMethod::Nip98,
11591174
};
11601175

11611176
// Typing indicators are ephemeral: broadcast to the channel, never stored.
1162-
let result = if kind_u32 == buzz_core::kind::KIND_TYPING_INDICATOR {
1177+
let result = if is_typing {
11631178
let event_id = event.id.to_hex();
11641179
crate::handlers::event::publish_http_typing(state, tenant, event, pubkey)
11651180
.await
@@ -8308,6 +8323,53 @@ pub(crate) mod postgres_tests {
83088323
fx.drop().await;
83098324
}
83108325

8326+
/// HTTP agents reach `materialize_nip_oa_owner` on every request. Once the
8327+
/// owner link is confirmed in `observer_owner_cache` the call writes
8328+
/// nothing: the link is set once and never cleared.
8329+
/// Mutation: drop the cache short-circuit → the agent row is recreated → RED.
8330+
#[tokio::test]
8331+
#[ignore = "requires Postgres and Redis"]
8332+
async fn known_owner_link_skips_materialize_writes() {
8333+
let state = bridge_handler_test_state()
8334+
.await
8335+
.expect("local Postgres and Redis");
8336+
let host = format!("owner-link-{}.local", uuid::Uuid::new_v4());
8337+
state.db.ensure_configured_community(&host).await.unwrap();
8338+
let tenant = crate::tenant::bind_community(&state.db, &host)
8339+
.await
8340+
.unwrap();
8341+
let (owner, agent_key) = (Keys::generate().public_key(), Keys::generate().public_key());
8342+
let materialize = || {
8343+
super::super::relay_members::materialize_nip_oa_owner(
8344+
&state, &tenant, &agent_key, &owner,
8345+
)
8346+
};
8347+
let agent_rows = || async {
8348+
sqlx::query_scalar::<_, i64>(
8349+
"SELECT COUNT(*) FROM users WHERE community_id = $1 AND pubkey = $2",
8350+
)
8351+
.bind(tenant.community().as_uuid())
8352+
.bind(agent_key.to_bytes().as_slice())
8353+
.fetch_one(state.db.pool())
8354+
.await
8355+
.unwrap()
8356+
};
8357+
8358+
assert!(materialize().await, "first call records the link");
8359+
assert_eq!(agent_rows().await, 1);
8360+
8361+
// Remove the row behind the cache's back: a call that still wrote
8362+
// would recreate it.
8363+
sqlx::query("DELETE FROM users WHERE community_id = $1 AND pubkey = $2")
8364+
.bind(tenant.community().as_uuid())
8365+
.bind(agent_key.to_bytes().as_slice())
8366+
.execute(state.db.pool())
8367+
.await
8368+
.unwrap();
8369+
assert!(materialize().await, "a known link is still confirmed");
8370+
assert_eq!(agent_rows().await, 0, "a known link writes nothing");
8371+
}
8372+
83118373
/// An agent socket admitted with no owner, then linked to its owner by a
83128374
/// later `POST /events` carrying NIP-OA, closes when the owner is banned
83138375
/// or removed even though the owner-to-agent lookup fails: recording the

‎crates/buzz-relay/src/api/mod.rs‎

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,47 @@ pub mod relay_members {
246246
pubkey_bytes: &[u8],
247247
auth_tag_header: Option<&str>,
248248
signed_auth_created_at: Option<u64>,
249+
) -> Result<Option<nostr::PublicKey>, (StatusCode, Json<serde_json::Value>)> {
250+
enforce_membership(
251+
state,
252+
community,
253+
pubkey_bytes,
254+
auth_tag_header,
255+
signed_auth_created_at,
256+
false,
257+
)
258+
.await
259+
}
260+
261+
/// [`enforce_relay_membership`] for HTTP typing pulses: the ban check
262+
/// reads through the 30-second ephemeral-path restriction cache, the same
263+
/// staleness bound the pulse's own write-restriction check already has.
264+
/// Every other HTTP request reads ban state fresh.
265+
pub async fn enforce_relay_membership_ephemeral(
266+
state: &AppState,
267+
community: CommunityId,
268+
pubkey_bytes: &[u8],
269+
auth_tag_header: Option<&str>,
270+
signed_auth_created_at: Option<u64>,
271+
) -> Result<Option<nostr::PublicKey>, (StatusCode, Json<serde_json::Value>)> {
272+
enforce_membership(
273+
state,
274+
community,
275+
pubkey_bytes,
276+
auth_tag_header,
277+
signed_auth_created_at,
278+
true,
279+
)
280+
.await
281+
}
282+
283+
async fn enforce_membership(
284+
state: &AppState,
285+
community: CommunityId,
286+
pubkey_bytes: &[u8],
287+
auth_tag_header: Option<&str>,
288+
signed_auth_created_at: Option<u64>,
289+
cached_ban: bool,
249290
) -> Result<Option<nostr::PublicKey>, (StatusCode, Json<serde_json::Value>)> {
250291
match check_relay_membership(
251292
state,
@@ -263,6 +304,7 @@ pub mod relay_members {
263304
pubkey_bytes,
264305
auth_tag_header,
265306
signed_auth_created_at,
307+
cached_ban,
266308
)
267309
.await?;
268310
Ok(None)
@@ -274,6 +316,7 @@ pub mod relay_members {
274316
pubkey_bytes,
275317
auth_tag_header,
276318
signed_auth_created_at,
319+
cached_ban,
277320
)
278321
.await?;
279322
Ok(Some(owner))
@@ -301,6 +344,7 @@ pub mod relay_members {
301344
pubkey_bytes: &[u8],
302345
auth_tag_header: Option<&str>,
303346
signed_auth_created_at: Option<u64>,
347+
cached: bool,
304348
) -> Result<(), (StatusCode, Json<serde_json::Value>)> {
305349
let Ok(pubkey) = nostr::PublicKey::from_slice(pubkey_bytes) else {
306350
return Err(super::internal_error("invalid pubkey for ban check"));
@@ -311,6 +355,7 @@ pub mod relay_members {
311355
pubkey,
312356
auth_tag_header,
313357
signed_auth_created_at,
358+
cached,
314359
)
315360
.await
316361
{
@@ -359,12 +404,24 @@ pub mod relay_members {
359404
/// Both principals are ensured first because `agent_owner_pubkey` has a
360405
/// community-scoped foreign key. The mapping is first-write-wins; an
361406
/// existing mapping is accepted only when it names the same owner.
407+
///
408+
/// A mapping already confirmed in `observer_owner_cache` returns early:
409+
/// `agent_owner_pubkey` is set once and never cleared, so the writes
410+
/// below would all be no-ops. HTTP agents call this on every request.
362411
pub async fn materialize_nip_oa_owner(
363412
state: &AppState,
364413
tenant: &TenantContext,
365414
agent: &nostr::PublicKey,
366415
owner: &nostr::PublicKey,
367416
) -> bool {
417+
let owner_key = (
418+
tenant.community(),
419+
agent.to_bytes().to_vec(),
420+
owner.to_bytes().to_vec(),
421+
);
422+
if state.observer_owner_cache.get(&owner_key) == Some(true) {
423+
return true;
424+
}
368425
for (role, pubkey) in [("agent", agent), ("owner", owner)] {
369426
match state
370427
.db
@@ -417,14 +474,7 @@ pub mod relay_members {
417474
state
418475
.author_type_cache
419476
.insert((tenant.community(), agent.to_bytes().to_vec()), true);
420-
state.observer_owner_cache.insert(
421-
(
422-
tenant.community(),
423-
agent.to_bytes().to_vec(),
424-
owner.to_bytes().to_vec(),
425-
),
426-
true,
427-
);
477+
state.observer_owner_cache.insert(owner_key, true);
428478
}
429479
materialized
430480
}

‎crates/buzz-relay/src/api/typing_postgres_tests.rs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,62 @@ async fn typing_uses_cached_restriction_row() {
176176
assert_eq!(status, axum::http::StatusCode::FORBIDDEN, "{body}");
177177
}
178178

179+
/// The membership-stage ban gate for HTTP typing reads the same cache: a
180+
/// cached clear row admits a member Postgres banned within the TTL.
181+
/// Mutation: route typing through `enforce_relay_membership` (fresh ban
182+
/// read) → RED.
183+
#[tokio::test]
184+
#[ignore = "requires Postgres and Redis"]
185+
async fn typing_ban_gate_uses_cached_restriction_row() {
186+
let f = Fixture::new().await;
187+
let member = f.member.public_key().to_bytes().to_vec();
188+
f.state
189+
.db
190+
.ban_community_member(
191+
f.community,
192+
&member,
193+
Keys::generate().public_key().as_bytes(),
194+
None,
195+
None,
196+
)
197+
.await
198+
.unwrap();
199+
f.state.restriction_cache.insert(
200+
(f.community, member),
201+
buzz_db::moderation::RestrictionState {
202+
banned: false,
203+
muted_until: None,
204+
},
205+
);
206+
let (status, body) = f
207+
.post(&f.member, &f.typing(&f.member, Some(f.channel)))
208+
.await;
209+
assert_eq!(status, axum::http::StatusCode::OK, "{body}");
210+
}
211+
212+
/// Only typing uses the cached ban gate: a persistent post reads ban state
213+
/// fresh, so a stale cached ban does not refuse a member Postgres cleared.
214+
/// Mutation: use the cached ban gate for every kind → RED.
215+
#[tokio::test]
216+
#[ignore = "requires Postgres and Redis"]
217+
async fn persistent_post_ban_gate_reads_fresh() {
218+
let f = Fixture::new().await;
219+
f.state.restriction_cache.insert(
220+
(f.community, f.member.public_key().to_bytes().to_vec()),
221+
buzz_db::moderation::RestrictionState {
222+
banned: true,
223+
muted_until: None,
224+
},
225+
);
226+
let message = EventBuilder::new(Kind::Custom(9), "hello")
227+
.tags([Tag::parse(["h".to_string(), f.channel.to_string()]).unwrap()])
228+
.sign_with_keys(&f.member)
229+
.unwrap();
230+
let (status, body) = f.post(&f.member, &message).await;
231+
assert_eq!(status, axum::http::StatusCode::OK, "{body}");
232+
assert_eq!(body["accepted"], true, "{body}");
233+
}
234+
179235
/// HTTP typing reads the cached serving state, like the WebSocket path.
180236
/// Mutation: call the uncached `is_serving_active` → RED.
181237
#[tokio::test]

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

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,35 @@ fn classify_relay_membership(
6262
/// Fails closed: a DB error is `DbError`, never `Clear`. NIP-OA cascade: a ban
6363
/// on the principal blocks it directly; if the principal is clear, a ban on
6464
/// its proven owner (extracted from the self-proving auth tag) blocks it too.
65+
///
66+
/// `cached` reads the restriction rows through the 30-second ephemeral-path
67+
/// cache ([`AppState::restriction_state_cached`]); only per-pulse ephemeral
68+
/// traffic sets it. Socket auth and every other HTTP request read fresh.
6569
pub(crate) async fn community_ban_outcome(
6670
state: &AppState,
6771
community: buzz_core::CommunityId,
6872
pubkey: nostr::PublicKey,
6973
auth_tag_json: Option<&str>,
7074
signed_auth_created_at: Option<u64>,
75+
cached: bool,
7176
) -> BanOutcome {
7277
async fn lookup(
7378
state: &AppState,
7479
community: buzz_core::CommunityId,
7580
key: &nostr::PublicKey,
81+
cached: bool,
7682
) -> BanOutcome {
77-
match state
78-
.db
79-
.moderation_restriction_state(community, key.as_bytes())
80-
.await
81-
{
83+
let restriction = if cached {
84+
state
85+
.restriction_state_cached(community, key.as_bytes())
86+
.await
87+
} else {
88+
state
89+
.db
90+
.moderation_restriction_state(community, key.as_bytes())
91+
.await
92+
};
93+
match restriction {
8294
Ok(restriction) if restriction.banned => BanOutcome::Banned,
8395
Ok(_) => BanOutcome::Clear,
8496
Err(e) => {
@@ -87,7 +99,7 @@ pub(crate) async fn community_ban_outcome(
8799
}
88100
}
89101
}
90-
let outcome = lookup(state, community, &pubkey).await;
102+
let outcome = lookup(state, community, &pubkey, cached).await;
91103
if outcome != BanOutcome::Clear {
92104
return outcome;
93105
}
@@ -96,7 +108,7 @@ pub(crate) async fn community_ban_outcome(
96108
auth_tag_json,
97109
signed_auth_created_at,
98110
) {
99-
Some(owner) => lookup(state, community, &owner).await,
111+
Some(owner) => lookup(state, community, &owner, cached).await,
100112
None => BanOutcome::Clear,
101113
}
102114
}
@@ -146,6 +158,7 @@ pub(crate) async fn final_admission_denial(
146158
pubkey,
147159
auth_tag_json,
148160
signed_auth_created_at,
161+
false,
149162
)
150163
.await;
151164
if let Some((metric, reason, outcome)) = ban_denial(ban) {
@@ -402,6 +415,7 @@ pub async fn handle_auth(event: nostr::Event, conn: Arc<ConnectionState>, state:
402415
pubkey,
403416
auth_tag_json.as_deref(),
404417
Some(signed_auth_created_at),
418+
false,
405419
)
406420
.await;
407421

@@ -877,7 +891,7 @@ mod tests {
877891
// Fix 5: use Config::for_test() which holds NIP_FI_ENV_LOCK internally. [FI-TRACE-ENV-RACE]
878892
let mut config = crate::config::Config::for_test();
879893
config.require_relay_membership = false;
880-
config.database_url = "postgres://buzz:[email protected]:1/buzz".to_string();
894+
config.database_url = "postgres://buzz:[email protected]:1/buzz".to_string(); // sadscan:disable np.postgres.1
881895
config.redis_url = "redis://127.0.0.1:1".to_string();
882896
// 100ms acquire timeout: a request that falls through to the stub
883897
// pool still waits, but for 100ms instead of sqlx's 30s default.

0 commit comments

Comments
 (0)