Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ REDIS_URL=redis://localhost:6379
# Max connections in the relay's shared Redis pool (default 16).
# BUZZ_REDIS_POOL_SIZE=16

# Max connections in each of the relay's Postgres pools — writer and, when
# READ_DATABASE_URL is set, reader (default 50).
# BUZZ_DB_POOL_SIZE=50

# -----------------------------------------------------------------------------
# Typesense (search)
# -----------------------------------------------------------------------------
Expand Down
41 changes: 41 additions & 0 deletions crates/buzz-relay/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ pub struct Config {
/// pod is only 4 — small enough that rate-limit checks, presence, and
/// pub/sub publishes queue behind each other under load.
pub redis_pool_size: usize,
/// Maximum connections in the Postgres writer/reader pools. Defaults to 50.
///
/// The `buzz-db` default of 20 was sized for a handful of pods against
/// `max_connections=100`. Against Aurora (~5,000 connections) that cap
/// is the binding constraint: a burst of concurrent handlers exhausts
/// the per-pod pool and requests fail on acquire timeout while the
/// database sits idle.
pub db_pool_size: u32,
/// Public WebSocket URL of this relay, advertised in NIP-11.
pub relay_url: String,
/// Public WebSocket URL of the dedicated device-pairing relay, when configured.
Expand Down Expand Up @@ -424,6 +432,12 @@ impl Config {
.filter(|&v| v > 0)
.unwrap_or(16);

let db_pool_size = std::env::var("BUZZ_DB_POOL_SIZE")
.ok()
.and_then(|v| v.parse::<u32>().ok())
.filter(|&v| v > 0)
.unwrap_or(50);

let relay_url =
std::env::var("RELAY_URL").unwrap_or_else(|_| "ws://localhost:3000".to_string());

Expand Down Expand Up @@ -875,6 +889,7 @@ impl Config {
read_database_url,
redis_url,
redis_pool_size,
db_pool_size,
relay_url,
pairing_relay_url,
max_connections,
Expand Down Expand Up @@ -942,6 +957,7 @@ mod tests {
assert!(!config.database_url.is_empty());
assert!(!config.redis_url.is_empty());
assert_eq!(config.redis_pool_size, 16);
assert_eq!(config.db_pool_size, 50);
assert!(config.max_connections > 0);
assert!(config.send_buffer_size > 0);
assert_eq!(config.max_frame_bytes, DEFAULT_MAX_FRAME_BYTES);
Expand Down Expand Up @@ -1009,6 +1025,31 @@ mod tests {
assert_eq!(junk, 16, "unparsable value must fall back to the default");
}

#[test]
fn db_pool_size_env_override_and_invalid_fallback() {
let _guard = ENV_MUTEX.lock().unwrap();
let previous = std::env::var_os("BUZZ_DB_POOL_SIZE");

std::env::set_var("BUZZ_DB_POOL_SIZE", "80");
let overridden = Config::from_env().expect("config").db_pool_size;

std::env::set_var("BUZZ_DB_POOL_SIZE", "0");
let zero = Config::from_env().expect("config").db_pool_size;

std::env::set_var("BUZZ_DB_POOL_SIZE", "not-a-number");
let junk = Config::from_env().expect("config").db_pool_size;

if let Some(value) = previous {
std::env::set_var("BUZZ_DB_POOL_SIZE", value);
} else {
std::env::remove_var("BUZZ_DB_POOL_SIZE");
}

assert_eq!(overridden, 80);
assert_eq!(zero, 50, "zero must fall back to the default");
assert_eq!(junk, 50, "unparsable value must fall back to the default");
}

#[test]
fn read_database_url_unset_or_blank_is_none() {
let _guard = ENV_MUTEX.lock().unwrap();
Expand Down
1 change: 1 addition & 0 deletions crates/buzz-relay/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ async fn main() -> anyhow::Result<()> {
let db_config = DbConfig {
database_url: config.database_url.clone(),
read_database_url: config.read_database_url.clone(),
max_connections: config.db_pool_size,
..DbConfig::default()
};
let db = Db::new(&db_config).await.map_err(|e| {
Expand Down
Loading