Skip to content

Commit 03ba494

Browse files
tlongwell-blocknpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d
andauthored
perf(relay): serve relay-membership checks from the read replica (#4124)
## Summary Route `Db::is_relay_member` — the membership check that runs on every authenticated HTTP request and WS AUTH — through the standard `route_read` machinery on the bounded arm, instead of adding a bespoke cache (replaces #3844). - `crates/buzz-db/src/relay_members.rs`: add `is_relay_member_on(&mut PgConnection, ...)` executor seam; the pool version delegates to it. - `crates/buzz-db/src/lib.rs`: `Db::is_relay_member` now routes via `route_read("relay_membership", RoutePredicate::Bounded)` — replica only on a proved fresh session, writer on any route rejection, writer re-run on replica query error. Exactly the shape of every other routed read. This is the one permission read served from the replica, by explicit product decision (Tyler accepted ≤1s bounded staleness on reads we choose): the fleet-wide fence guarantee (`BUZZ_REPLICA_READ_MAX_AGE_MS`, deploy target 1s) is an order of magnitude tighter than the 10s TTL proposed in #3844 and needs no invalidation machinery. Staleness is symmetric for admits and revokes. `BUZZ_REPLICA_READ_MAX_AGE_MS` unset = writer-only = kill switch. It is not precedent for routing other permission reads. ## Validation At this exact commit (`git rev-parse HEAD` confirmed in the same shell, rustc 1.95): - `cargo test -p buzz-db` — 94 passed, 0 failed - PG-gated suite single-threaded — **151 passed, 2 failed**; the 2 failures are the per-owner-limit tests broken on main by #3829 (limit 3→5, tests still seed 3) — they fail identically at base `37d1a76b4` in a pristine control checkout; separate trivial fix to follow - New PG-gated test `is_relay_member_is_bounded_routed_and_fails_closed` — divergent writer/replica fixtures prove: budget unset ⇒ writer; budget set + fresh proof ⇒ replica; over-budget entry ⇒ writer - clippy `-D warnings` + fmt clean; pre-push hooks green (desktop check/test, rust tests, tauri checks) - **Live-local pass** (TESTING.md, release binary, `BUZZ_REQUIRE_RELAY_MEMBERSHIP=true`, fresh DB): - writer-only (no `READ_DATABASE_URL`): member accepted, outsider 403 `relay_membership_required`; metrics `route_decision{path="relay_membership",decision="writer",reason="disabled"}` - replica configured + `BUZZ_REPLICA_READ_MAX_AGE_MS=1000`: member accepted / outsider denied via `decision="replica",reason="fresh"`; admit visible to the routed check within ~1.2s; revoke enforced within ~1.2s - reader outage mid-flight (TCP proxy killed): member send still succeeds in <200ms via `decision="writer",reason="reader_acquire_timeout"`; outsider still denied — fails closed, no availability loss Reviewed by Wren: 9/10 minimalness, 9/10 elegance, 9.5/10 correctness at this SHA. Supersedes the 10s-cache approach in PR 3844, which should be closed unmerged once this lands. Signed-off-by: Tyler <[email protected]> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@buzz.block.builderlab.xyz>
1 parent 525398e commit 03ba494

2 files changed

Lines changed: 111 additions & 2 deletions

File tree

‎crates/buzz-db/src/lib.rs‎

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3999,8 +3999,33 @@ impl Db {
39993999
}
40004000

40014001
/// Returns `true` if `pubkey` (64-char hex) is a member of `community`.
4002+
///
4003+
/// Replica-routed on the bounded arm — the one PERMISSION read routed by
4004+
/// explicit product decision (bounded-stale membership beats the 10s
4005+
/// cache it replaced). Admits and revokes may lag by at most the budget
4006+
/// `B`; everything else fails closed to the writer, exactly like
4007+
/// [`Db::query_events_routed_bounded`]. Not precedent for routing other
4008+
/// permission reads.
40024009
pub async fn is_relay_member(&self, community: CommunityId, pubkey: &str) -> Result<bool> {
4003-
relay_members::is_relay_member(&self.pool, community, pubkey).await
4010+
let path = "relay_membership";
4011+
match self.route_read(path, RoutePredicate::Bounded).await {
4012+
RouteDecision::Replica(mut tx, _entry, reason) => {
4013+
match relay_members::is_relay_member_on(&mut tx, community, pubkey).await {
4014+
Ok(is_member) => {
4015+
Self::record_route(path, "replica", reason);
4016+
Ok(is_member)
4017+
}
4018+
Err(e) => {
4019+
tracing::warn!(path, "replica read failed; re-running on writer: {e}");
4020+
Self::record_route(path, "writer", "replica_error");
4021+
relay_members::is_relay_member(&self.pool, community, pubkey).await
4022+
}
4023+
}
4024+
}
4025+
RouteDecision::Writer => {
4026+
relay_members::is_relay_member(&self.pool, community, pubkey).await
4027+
}
4028+
}
40044029
}
40054030

40064031
/// Returns the relay member record for `pubkey` in `community`, or `None` if not found.
@@ -7414,6 +7439,78 @@ mod tests {
74147439
drop_scratch_db(&admin, writer, &wname).await;
74157440
}
74167441

7442+
/// Routed relay-membership check: budget unset ⇒ writer; budget set +
7443+
/// fresh proved entry ⇒ replica (bounded arm); over-budget entry ⇒
7444+
/// writer. Divergent membership rows prove which pool answered.
7445+
#[tokio::test]
7446+
#[ignore = "requires Postgres"]
7447+
async fn is_relay_member_is_bounded_routed_and_fails_closed() {
7448+
let admin = PgPool::connect(&admin_url().await)
7449+
.await
7450+
.expect("connect admin");
7451+
let (writer, wname) = create_scratch_db(&admin, "mem_w").await;
7452+
let (replica, rname) = create_scratch_db(&admin, "mem_r").await;
7453+
7454+
let community = Uuid::new_v4();
7455+
for pool in [&writer, &replica] {
7456+
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
7457+
.bind(community)
7458+
.bind(format!("member-routing-{}.example", community.simple()))
7459+
.execute(pool)
7460+
.await
7461+
.expect("insert community");
7462+
}
7463+
let cid = CommunityId::from_uuid(community);
7464+
let writer_only = "aa".repeat(32);
7465+
let replica_only = "bb".repeat(32);
7466+
relay_members::add_relay_member(&writer, cid, &writer_only, "member", None)
7467+
.await
7468+
.expect("seed writer member");
7469+
relay_members::add_relay_member(&replica, cid, &replica_only, "member", None)
7470+
.await
7471+
.expect("seed replica member");
7472+
7473+
let mut db = Db::from_pools(writer.clone(), replica.clone());
7474+
db.fence().force_open_for_tests(chrono::Utc::now());
7475+
7476+
// Budget unset ⇒ bounded arm disabled ⇒ writer.
7477+
assert!(
7478+
db.is_relay_member(cid, &writer_only)
7479+
.await
7480+
.expect("gate off"),
7481+
"budget unset must answer from the writer"
7482+
);
7483+
assert!(!db.is_relay_member(cid, &replica_only).await.unwrap());
7484+
7485+
// Budget set + fresh entry ⇒ replica.
7486+
db.set_replica_read_max_age_for_tests(Some(std::time::Duration::from_secs(5)));
7487+
assert!(
7488+
db.is_relay_member(cid, &replica_only)
7489+
.await
7490+
.expect("gate on"),
7491+
"budget set must answer from the replica"
7492+
);
7493+
assert!(!db.is_relay_member(cid, &writer_only).await.unwrap());
7494+
7495+
// Entry older than the budget ⇒ fail closed to the writer. Close
7496+
// first so no prior fresh entry can be the one proved (matches the
7497+
// count test; today `force_open_for_tests_at` also clears the ring).
7498+
db.fence().close();
7499+
db.fence().force_open_for_tests_at(
7500+
chrono::Utc::now(),
7501+
std::time::Instant::now() - std::time::Duration::from_secs(10),
7502+
);
7503+
assert!(
7504+
db.is_relay_member(cid, &writer_only)
7505+
.await
7506+
.expect("entry too old"),
7507+
"an over-budget entry must fail closed to the writer"
7508+
);
7509+
7510+
drop_scratch_db(&admin, replica, &rname).await;
7511+
drop_scratch_db(&admin, writer, &wname).await;
7512+
}
7513+
74177514
/// Community separation across every routed seam, verified on
74187515
/// REPLICA-SERVED reads.
74197516
///

‎crates/buzz-db/src/relay_members.rs‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,22 @@ pub struct RelayMember {
2929

3030
/// Returns `true` if `pubkey` (64-char hex) is a member of `community`.
3131
pub async fn is_relay_member(pool: &PgPool, community: CommunityId, pubkey: &str) -> Result<bool> {
32+
let mut conn = pool.acquire().await?;
33+
is_relay_member_on(&mut conn, community, pubkey).await
34+
}
35+
36+
/// [`is_relay_member`] on a specific session — the replica-routing path runs
37+
/// the lookup on the exact reader connection whose heartbeat observation
38+
/// proved fence coverage.
39+
pub(crate) async fn is_relay_member_on(
40+
conn: &mut sqlx::PgConnection,
41+
community: CommunityId,
42+
pubkey: &str,
43+
) -> Result<bool> {
3244
let row = sqlx::query("SELECT 1 FROM relay_members WHERE community_id = $1 AND pubkey = $2")
3345
.bind(community.as_uuid())
3446
.bind(pubkey)
35-
.fetch_optional(pool)
47+
.fetch_optional(conn)
3648
.await?;
3749
Ok(row.is_some())
3850
}

0 commit comments

Comments
 (0)