Skip to content

Commit 9f98072

Browse files
TheSentinel454Elrondclaude
committed
fix(relay): bound the bulk membership read per user, and pin it
Read each pubkey's memberships with a lateral LIMIT, so each user's scan stops at the cap like the single-user read. Pin the cap per user with two users above a positive cap, and state the closer's 10/s floor next to its 1% share. Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-authored-by: Claude Opus 5.5 <[email protected]> Signed-off-by: Luke Tornquist <[email protected]>
1 parent 99469c7 commit 9f98072

3 files changed

Lines changed: 37 additions & 13 deletions

File tree

‎crates/buzz-db/src/store/channel_members.rs‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,14 +1279,16 @@ pub async fn get_active_membership_ids_bulk_read_at(
12791279
)
12801280
.await?;
12811281
let read_at = std::time::Instant::now();
1282+
// One bounded read per pubkey, as in the single-user query: each user's
1283+
// scan stops at `limit` rows.
12821284
let rows = sqlx::query(
1283-
"SELECT pubkey, channel_id FROM ( \
1284-
SELECT cm.pubkey, cm.channel_id, \
1285-
ROW_NUMBER() OVER (PARTITION BY cm.pubkey) AS n \
1286-
FROM channel_members cm \
1285+
"SELECT u.pubkey, m.channel_id FROM unnest($2::bytea[]) AS u(pubkey) \
1286+
CROSS JOIN LATERAL ( \
1287+
SELECT cm.channel_id FROM channel_members cm \
12871288
JOIN channels c ON cm.community_id = c.community_id AND cm.channel_id = c.id AND c.deleted_at IS NULL \
1288-
WHERE cm.community_id = $1 AND cm.pubkey = ANY($2) AND cm.removed_at IS NULL \
1289-
) m WHERE n <= $3",
1289+
WHERE cm.community_id = $1 AND cm.pubkey = u.pubkey AND cm.removed_at IS NULL \
1290+
LIMIT $3 \
1291+
) m",
12901292
)
12911293
.bind(community_id.as_uuid())
12921294
.bind(pubkeys)

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,11 @@ pub const WORKER_QUEUE: usize = 1024;
7575
/// of a few seconds, so such a stall delays deliveries instead of closing
7676
/// their recipients.
7777
pub const MAX_DELIVERY_DELAY: Duration = Duration::from_secs(30);
78-
/// The share of this node's connections the closer may close per second:
79-
/// 1%, so closing every connection takes at least 100 s.
78+
/// The share of this node's subscribed connections the closer may close per
79+
/// second, 1%, above a floor of one per [`CLOSE_TICK`]: `max(1% of
80+
/// connections, 10)` closes per second. From 1,000 connections up, closing
81+
/// every connection takes at least 100 s; a smaller node is bounded by the
82+
/// floor (150 connections close in about 15 s).
8083
const CLOSE_SHARE_PER_SECOND: f64 = 0.01;
8184
/// The closer's mean tick. Each tick is jittered by ±50%.
8285
const CLOSE_TICK: Duration = Duration::from_millis(100);
@@ -259,9 +262,10 @@ fn close_rate(connections: usize) -> f64 {
259262
}
260263

261264
/// Fences connections that lost a live event at once, then closes them at
262-
/// [`CLOSE_SHARE_PER_SECOND`] of this node's connections, at least one per
263-
/// tick. Every node paces its own closes, so a loss that reaches every node
264-
/// still sends at most that share of the fleet to reconnect each second. The
265+
/// [`close_rate`]: [`CLOSE_SHARE_PER_SECOND`] of this node's connections, at
266+
/// least one per tick. Every node paces its own closes, so a loss that reaches
267+
/// every node sends at most that share of the fleet (or the floor, on nodes
268+
/// under 1,000 connections) to reconnect each second. The
265269
/// pending set holds each connection once and never more than this node's
266270
/// connections.
267271
#[derive(Default)]
@@ -497,6 +501,11 @@ mod tests {
497501
#[tokio::test(start_paused = true)]
498502
async fn closer_closes_a_share_of_the_nodes_connections_per_second() {
499503
assert_eq!(close_rate(100), 10.0, "the floor: one per mean tick");
504+
assert_eq!(
505+
close_rate(150),
506+
10.0,
507+
"a small node is bounded by the floor"
508+
);
500509
assert_eq!(close_rate(10_000), 100.0, "1% of the node per second");
501510

502511
let state = crate::state::tests::test_state().await;

‎crates/buzz-relay/src/membership_cache/postgres_tests.rs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,12 +354,25 @@ async fn fan_out_bulk_read_matches_single_user_membership() {
354354
);
355355
}
356356

357+
// Two users above a positive cap each get exactly the cap: a cap on the
358+
// whole batch would truncate one of them into false denials.
359+
let heavy = [vec![5u8; 32], vec![6u8; 32]];
360+
let three = [Uuid::new_v4(), Uuid::new_v4(), Uuid::new_v4()];
361+
sqlx::query("INSERT INTO channels (community_id, id, name, visibility, created_by) SELECT $1, id, id::text, 'private', $3 FROM unnest($2::uuid[]) AS id")
362+
.bind(community.as_uuid()).bind(three).bind(&member).execute(&pool).await.unwrap();
363+
for pubkey in &heavy {
364+
sqlx::query("INSERT INTO channel_members (community_id, channel_id, pubkey) SELECT $1, id, $3 FROM unnest($2::uuid[]) AS id")
365+
.bind(community.as_uuid()).bind(three).bind(pubkey).execute(&pool).await.unwrap();
366+
}
357367
let capped = state
358368
.db
359-
.get_active_membership_ids_bulk_read_at(community, &[member.clone(), removed.clone()], 0)
369+
.get_active_membership_ids_bulk_read_at(community, &heavy, 2)
360370
.await
361371
.unwrap();
362-
assert!(capped.value.is_empty(), "the cap applies per user");
372+
for pubkey in &heavy {
373+
let rows = capped.value.iter().filter(|(pk, _)| pk == pubkey).count();
374+
assert_eq!(rows, 2, "the cap applies per user");
375+
}
363376

364377
pool.close().await;
365378
let uncached = vec![9u8; 32];

0 commit comments

Comments
 (0)