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
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ PGDATABASE=buzz
# Redis 7
# -----------------------------------------------------------------------------
REDIS_URL=redis://localhost:6379
# Max connections in the relay's shared Redis pool (default 16).
# BUZZ_REDIS_POOL_SIZE=16

# -----------------------------------------------------------------------------
# Typesense (search)
Expand Down
39 changes: 39 additions & 0 deletions crates/buzz-relay/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ pub struct Config {
pub read_database_url: Option<String>,
/// Redis connection URL used by the pub/sub manager.
pub redis_url: String,
/// Maximum connections in the shared Redis pool. Defaults to 16.
///
/// deadpool's own default is `CPU_COUNT * 2`, which on a 2-vCPU relay
/// 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,
/// 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 @@ -412,6 +418,12 @@ impl Config {
let redis_url =
std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());

let redis_pool_size = std::env::var("BUZZ_REDIS_POOL_SIZE")
.ok()
.and_then(|v| v.parse::<usize>().ok())
.filter(|&v| v > 0)
.unwrap_or(16);

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

Expand Down Expand Up @@ -862,6 +874,7 @@ impl Config {
database_url,
read_database_url,
redis_url,
redis_pool_size,
relay_url,
pairing_relay_url,
max_connections,
Expand Down Expand Up @@ -928,6 +941,7 @@ mod tests {
assert!(config.bind_addr.port() > 0);
assert!(!config.database_url.is_empty());
assert!(!config.redis_url.is_empty());
assert_eq!(config.redis_pool_size, 16);
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 @@ -970,6 +984,31 @@ mod tests {
);
}

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

std::env::set_var("BUZZ_REDIS_POOL_SIZE", "32");
let overridden = Config::from_env().expect("config").redis_pool_size;

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

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

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

assert_eq!(overridden, 32);
assert_eq!(zero, 16, "zero must fall back to the default");
assert_eq!(junk, 16, "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
3 changes: 2 additions & 1 deletion crates/buzz-relay/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ async fn main() -> anyhow::Result<()> {
};

let redis_pool = {
let cfg = deadpool_redis::Config::from_url(&config.redis_url);
let mut cfg = deadpool_redis::Config::from_url(&config.redis_url);
cfg.pool = Some(deadpool_redis::PoolConfig::new(config.redis_pool_size));
cfg.create_pool(Some(deadpool_redis::Runtime::Tokio1))
.map_err(|e| anyhow::anyhow!("Redis pool creation failed: {e}"))?
};
Expand Down
Loading