Skip to content

Commit 7ada3f9

Browse files
authored
feat(relay): make per-owner community limit configurable via BUZZ_MAX_COMMUNITIES_PER_OWNER (#2599)
Closes #2600 ## Summary Self-hosted multi-tenant deployments (one relay serving many communities via host-based tenancy) routinely need more than three communities owned by the same operator identity. `MAX_COMMUNITIES_PER_OWNER` is currently a hardcoded const, and hitting it surfaces as a `limit_reached` 409 from `POST /operator/communities` — which provisioning UIs tend to mislabel (mine reported it as "subdomain already taken"). This makes the limit configurable per deployment: - New env var `BUZZ_MAX_COMMUNITIES_PER_OWNER` — read once per process, must parse as a positive integer; missing/invalid/non-positive values fall back to the existing default of **3**, so current deployments are unaffected. - Enforcement locations are unchanged and stay in the authoritative relay-layer checks: community provisioning (`create_community_with_owner`) and ownership transfer (inside the advisory-lock transaction). - Parse/fallback rules are extracted into a pure helper (`effective_owner_limit`) with unit tests, keeping the cached getter trivial. ## Test plan - `cargo test -p buzz-db --lib` — new `owner_limit_*` tests cover default, invalid, non-positive, and positive-override cases. (Pre-existing unrelated failure on clean main: `replica_fence::tests::fence_starts_closed_and_opens_on_advance`, tracked in #2369.) - `cargo clippy -p buzz-db --all-targets` and `cargo fmt` clean. - Deployed on my multi-tenant relay (vibecode.casa) with `BUZZ_MAX_COMMUNITIES_PER_OWNER=100`: provisioning a 4th community for the same owner succeeds; without the var the stock limit of 3 still applies. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Lee Salminen <[email protected]>
1 parent 795f2d1 commit 7ada3f9

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,7 @@ impl Db {
898898
.fetch_one(&mut *tx)
899899
.await?;
900900

901-
if owned_count >= relay_members::MAX_COMMUNITIES_PER_OWNER {
901+
if owned_count >= relay_members::max_communities_per_owner() {
902902
tx.rollback().await?;
903903
return Ok(CreateCommunityWithOwnerResult::LimitReached);
904904
}

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

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,37 @@ pub enum TransferResult {
373373
LimitReached,
374374
}
375375

376-
/// Maximum number of communities a single pubkey can own. Enforced at the
377-
/// relay layer — the authoritative layer — so that concurrent transfers or
376+
/// Default maximum number of communities a single pubkey can own. Enforced at
377+
/// the relay layer — the authoritative layer — so that concurrent transfers or
378378
/// transfer-vs-create races cannot both pass a preflight count.
379379
pub const MAX_COMMUNITIES_PER_OWNER: i64 = 3;
380380

381+
/// Effective per-owner community limit for this deployment.
382+
///
383+
/// Reads `BUZZ_MAX_COMMUNITIES_PER_OWNER` once (cached for the process
384+
/// lifetime); a missing, unparsable, or non-positive value falls back to
385+
/// [`MAX_COMMUNITIES_PER_OWNER`]. Lets multi-tenant operators raise the cap
386+
/// without a source change while keeping the stock default for everyone else.
387+
pub fn max_communities_per_owner() -> i64 {
388+
static LIMIT: std::sync::OnceLock<i64> = std::sync::OnceLock::new();
389+
*LIMIT.get_or_init(|| {
390+
effective_owner_limit(
391+
std::env::var("BUZZ_MAX_COMMUNITIES_PER_OWNER")
392+
.ok()
393+
.as_deref(),
394+
)
395+
})
396+
}
397+
398+
/// Pure resolution of the owner limit from a raw env value — extracted from
399+
/// [`max_communities_per_owner`] so the parse/fallback rules are testable
400+
/// without process-global env state.
401+
fn effective_owner_limit(raw: Option<&str>) -> i64 {
402+
raw.and_then(|value| value.trim().parse::<i64>().ok())
403+
.filter(|limit| *limit > 0)
404+
.unwrap_or(MAX_COMMUNITIES_PER_OWNER)
405+
}
406+
381407
/// Stable advisory-lock key for serializing ownership-granting operations
382408
/// (transfer + create) per recipient pubkey. Uses FNV-1a over the hex pubkey
383409
/// so the same recipient always maps to the same lock across processes.
@@ -472,7 +498,7 @@ pub async fn transfer_ownership(
472498
.fetch_one(&mut *tx)
473499
.await?;
474500

475-
if owned_count >= MAX_COMMUNITIES_PER_OWNER {
501+
if owned_count >= max_communities_per_owner() {
476502
tx.rollback().await?;
477503
return Ok(TransferResult::LimitReached);
478504
}
@@ -555,6 +581,32 @@ pub async fn backfill_from_allowlist(pool: &PgPool, community: CommunityId) -> R
555581

556582
#[cfg(test)]
557583
mod tests {
584+
#[test]
585+
fn owner_limit_defaults_when_unset_or_invalid() {
586+
assert_eq!(
587+
super::effective_owner_limit(None),
588+
super::MAX_COMMUNITIES_PER_OWNER
589+
);
590+
assert_eq!(
591+
super::effective_owner_limit(Some("not-a-number")),
592+
super::MAX_COMMUNITIES_PER_OWNER
593+
);
594+
assert_eq!(
595+
super::effective_owner_limit(Some("0")),
596+
super::MAX_COMMUNITIES_PER_OWNER
597+
);
598+
assert_eq!(
599+
super::effective_owner_limit(Some("-5")),
600+
super::MAX_COMMUNITIES_PER_OWNER
601+
);
602+
}
603+
604+
#[test]
605+
fn owner_limit_honors_positive_override() {
606+
assert_eq!(super::effective_owner_limit(Some("100")), 100);
607+
assert_eq!(super::effective_owner_limit(Some(" 12 ")), 12);
608+
}
609+
558610
use super::*;
559611
use uuid::Uuid;
560612

0 commit comments

Comments
 (0)