From da039458c27c0760bf86436adace9e2785fe3c17 Mon Sep 17 00:00:00 2001 From: Lee Salminen Date: Thu, 23 Jul 2026 13:34:22 -0600 Subject: [PATCH] feat(relay): make per-owner community limit configurable Multi-tenant deployments (one relay serving many communities behind host-based tenancy) routinely need more than three communities per operator identity, but MAX_COMMUNITIES_PER_OWNER is a hardcoded const. The provisioning surface then rejects new communities with limit_reached, which downstream UIs tend to surface as a confusing "subdomain already taken". Introduce BUZZ_MAX_COMMUNITIES_PER_OWNER: read once per process, must be a positive integer, anything else falls back to the stock default of 3 so existing deployments are unaffected. Enforcement stays in the authoritative relay-layer checks (provision + transfer), unchanged. The parse/fallback rules live in a pure helper with unit tests; the cached getter stays trivial. Signed-off-by: Lee Salminen --- crates/buzz-db/src/lib.rs | 2 +- crates/buzz-db/src/relay_members.rs | 58 +++++++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/crates/buzz-db/src/lib.rs b/crates/buzz-db/src/lib.rs index fdd72c3c32..9c63b2e8ab 100644 --- a/crates/buzz-db/src/lib.rs +++ b/crates/buzz-db/src/lib.rs @@ -898,7 +898,7 @@ impl Db { .fetch_one(&mut *tx) .await?; - if owned_count >= relay_members::MAX_COMMUNITIES_PER_OWNER { + if owned_count >= relay_members::max_communities_per_owner() { tx.rollback().await?; return Ok(CreateCommunityWithOwnerResult::LimitReached); } diff --git a/crates/buzz-db/src/relay_members.rs b/crates/buzz-db/src/relay_members.rs index 3bd114fda0..3805745f9b 100644 --- a/crates/buzz-db/src/relay_members.rs +++ b/crates/buzz-db/src/relay_members.rs @@ -373,11 +373,37 @@ pub enum TransferResult { LimitReached, } -/// Maximum number of communities a single pubkey can own. Enforced at the -/// relay layer — the authoritative layer — so that concurrent transfers or +/// Default maximum number of communities a single pubkey can own. Enforced at +/// the relay layer — the authoritative layer — so that concurrent transfers or /// transfer-vs-create races cannot both pass a preflight count. pub const MAX_COMMUNITIES_PER_OWNER: i64 = 3; +/// Effective per-owner community limit for this deployment. +/// +/// Reads `BUZZ_MAX_COMMUNITIES_PER_OWNER` once (cached for the process +/// lifetime); a missing, unparsable, or non-positive value falls back to +/// [`MAX_COMMUNITIES_PER_OWNER`]. Lets multi-tenant operators raise the cap +/// without a source change while keeping the stock default for everyone else. +pub fn max_communities_per_owner() -> i64 { + static LIMIT: std::sync::OnceLock = std::sync::OnceLock::new(); + *LIMIT.get_or_init(|| { + effective_owner_limit( + std::env::var("BUZZ_MAX_COMMUNITIES_PER_OWNER") + .ok() + .as_deref(), + ) + }) +} + +/// Pure resolution of the owner limit from a raw env value — extracted from +/// [`max_communities_per_owner`] so the parse/fallback rules are testable +/// without process-global env state. +fn effective_owner_limit(raw: Option<&str>) -> i64 { + raw.and_then(|value| value.trim().parse::().ok()) + .filter(|limit| *limit > 0) + .unwrap_or(MAX_COMMUNITIES_PER_OWNER) +} + /// Stable advisory-lock key for serializing ownership-granting operations /// (transfer + create) per recipient pubkey. Uses FNV-1a over the hex pubkey /// so the same recipient always maps to the same lock across processes. @@ -472,7 +498,7 @@ pub async fn transfer_ownership( .fetch_one(&mut *tx) .await?; - if owned_count >= MAX_COMMUNITIES_PER_OWNER { + if owned_count >= max_communities_per_owner() { tx.rollback().await?; return Ok(TransferResult::LimitReached); } @@ -555,6 +581,32 @@ pub async fn backfill_from_allowlist(pool: &PgPool, community: CommunityId) -> R #[cfg(test)] mod tests { + #[test] + fn owner_limit_defaults_when_unset_or_invalid() { + assert_eq!( + super::effective_owner_limit(None), + super::MAX_COMMUNITIES_PER_OWNER + ); + assert_eq!( + super::effective_owner_limit(Some("not-a-number")), + super::MAX_COMMUNITIES_PER_OWNER + ); + assert_eq!( + super::effective_owner_limit(Some("0")), + super::MAX_COMMUNITIES_PER_OWNER + ); + assert_eq!( + super::effective_owner_limit(Some("-5")), + super::MAX_COMMUNITIES_PER_OWNER + ); + } + + #[test] + fn owner_limit_honors_positive_override() { + assert_eq!(super::effective_owner_limit(Some("100")), 100); + assert_eq!(super::effective_owner_limit(Some(" 12 ")), 12); + } + use super::*; use uuid::Uuid;