Skip to content

Commit bcc3e13

Browse files
tlongwell-blocknpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dTyler Longwell
authored
feat(relay): make Redis pool size configurable, default 16 (#2521)
Signed-off-by: Tyler Longwell <[email protected]> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@buzz.block.builderlab.xyz> Co-authored-by: Tyler Longwell <[email protected]>
1 parent acfbb1b commit bcc3e13

3 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎.env.example‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ PGDATABASE=buzz
3131
# Redis 7
3232
# -----------------------------------------------------------------------------
3333
REDIS_URL=redis://localhost:6379
34+
# Max connections in the relay's shared Redis pool (default 16).
35+
# BUZZ_REDIS_POOL_SIZE=16
3436

3537
# -----------------------------------------------------------------------------
3638
# Typesense (search)

‎crates/buzz-relay/src/config.rs‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ pub struct Config {
5858
pub read_database_url: Option<String>,
5959
/// Redis connection URL used by the pub/sub manager.
6060
pub redis_url: String,
61+
/// Maximum connections in the shared Redis pool. Defaults to 16.
62+
///
63+
/// deadpool's own default is `CPU_COUNT * 2`, which on a 2-vCPU relay
64+
/// pod is only 4 — small enough that rate-limit checks, presence, and
65+
/// pub/sub publishes queue behind each other under load.
66+
pub redis_pool_size: usize,
6167
/// Public WebSocket URL of this relay, advertised in NIP-11.
6268
pub relay_url: String,
6369
/// Public WebSocket URL of the dedicated device-pairing relay, when configured.
@@ -412,6 +418,12 @@ impl Config {
412418
let redis_url =
413419
std::env::var("REDIS_URL").unwrap_or_else(|_| "redis://localhost:6379".to_string());
414420

421+
let redis_pool_size = std::env::var("BUZZ_REDIS_POOL_SIZE")
422+
.ok()
423+
.and_then(|v| v.parse::<usize>().ok())
424+
.filter(|&v| v > 0)
425+
.unwrap_or(16);
426+
415427
let relay_url =
416428
std::env::var("RELAY_URL").unwrap_or_else(|_| "ws://localhost:3000".to_string());
417429

@@ -862,6 +874,7 @@ impl Config {
862874
database_url,
863875
read_database_url,
864876
redis_url,
877+
redis_pool_size,
865878
relay_url,
866879
pairing_relay_url,
867880
max_connections,
@@ -928,6 +941,7 @@ mod tests {
928941
assert!(config.bind_addr.port() > 0);
929942
assert!(!config.database_url.is_empty());
930943
assert!(!config.redis_url.is_empty());
944+
assert_eq!(config.redis_pool_size, 16);
931945
assert!(config.max_connections > 0);
932946
assert!(config.send_buffer_size > 0);
933947
assert_eq!(config.max_frame_bytes, DEFAULT_MAX_FRAME_BYTES);
@@ -970,6 +984,31 @@ mod tests {
970984
);
971985
}
972986

987+
#[test]
988+
fn redis_pool_size_env_override_and_invalid_fallback() {
989+
let _guard = ENV_MUTEX.lock().unwrap();
990+
let previous = std::env::var_os("BUZZ_REDIS_POOL_SIZE");
991+
992+
std::env::set_var("BUZZ_REDIS_POOL_SIZE", "32");
993+
let overridden = Config::from_env().expect("config").redis_pool_size;
994+
995+
std::env::set_var("BUZZ_REDIS_POOL_SIZE", "0");
996+
let zero = Config::from_env().expect("config").redis_pool_size;
997+
998+
std::env::set_var("BUZZ_REDIS_POOL_SIZE", "not-a-number");
999+
let junk = Config::from_env().expect("config").redis_pool_size;
1000+
1001+
if let Some(value) = previous {
1002+
std::env::set_var("BUZZ_REDIS_POOL_SIZE", value);
1003+
} else {
1004+
std::env::remove_var("BUZZ_REDIS_POOL_SIZE");
1005+
}
1006+
1007+
assert_eq!(overridden, 32);
1008+
assert_eq!(zero, 16, "zero must fall back to the default");
1009+
assert_eq!(junk, 16, "unparsable value must fall back to the default");
1010+
}
1011+
9731012
#[test]
9741013
fn read_database_url_unset_or_blank_is_none() {
9751014
let _guard = ENV_MUTEX.lock().unwrap();

‎crates/buzz-relay/src/main.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ async fn main() -> anyhow::Result<()> {
334334
};
335335

336336
let redis_pool = {
337-
let cfg = deadpool_redis::Config::from_url(&config.redis_url);
337+
let mut cfg = deadpool_redis::Config::from_url(&config.redis_url);
338+
cfg.pool = Some(deadpool_redis::PoolConfig::new(config.redis_pool_size));
338339
cfg.create_pool(Some(deadpool_redis::Runtime::Tokio1))
339340
.map_err(|e| anyhow::anyhow!("Redis pool creation failed: {e}"))?
340341
};

0 commit comments

Comments
 (0)