Skip to content

Commit 1b0aa0d

Browse files
author
npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d
committed
Merge origin/main into eva/replica-heartbeat-fence
* origin/main: feat(relay): make Postgres pool size configurable, default 50 (#3191) Co-authored-by: Tyler Longwell <[email protected]> Signed-off-by: Tyler Longwell <[email protected]> # Conflicts: # crates/buzz-relay/src/main.rs
2 parents fedb463 + 2ce2d71 commit 1b0aa0d

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

‎.env.example‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ REDIS_URL=redis://localhost:6379
3434
# Max connections in the relay's shared Redis pool (default 16).
3535
# BUZZ_REDIS_POOL_SIZE=16
3636

37+
# Max connections in each of the relay's Postgres pools — writer and, when
38+
# READ_DATABASE_URL is set, reader (default 50).
39+
# BUZZ_DB_POOL_SIZE=50
40+
3741
# -----------------------------------------------------------------------------
3842
# Typesense (search)
3943
# -----------------------------------------------------------------------------

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ pub struct Config {
6868
/// pod is only 4 — small enough that rate-limit checks, presence, and
6969
/// pub/sub publishes queue behind each other under load.
7070
pub redis_pool_size: usize,
71+
/// Maximum connections in the Postgres writer/reader pools. Defaults to 50.
72+
///
73+
/// The `buzz-db` default of 20 was sized for a handful of pods against
74+
/// `max_connections=100`. Against Aurora (~5,000 connections) that cap
75+
/// is the binding constraint: a burst of concurrent handlers exhausts
76+
/// the per-pod pool and requests fail on acquire timeout while the
77+
/// database sits idle.
78+
pub db_pool_size: u32,
7179
/// Public WebSocket URL of this relay, advertised in NIP-11.
7280
pub relay_url: String,
7381
/// Public WebSocket URL of the dedicated device-pairing relay, when configured.
@@ -439,6 +447,12 @@ impl Config {
439447
.filter(|&v| v > 0)
440448
.unwrap_or(16);
441449

450+
let db_pool_size = std::env::var("BUZZ_DB_POOL_SIZE")
451+
.ok()
452+
.and_then(|v| v.parse::<u32>().ok())
453+
.filter(|&v| v > 0)
454+
.unwrap_or(50);
455+
442456
let relay_url =
443457
std::env::var("RELAY_URL").unwrap_or_else(|_| "ws://localhost:3000".to_string());
444458

@@ -891,6 +905,7 @@ impl Config {
891905
replica_head_max_age_secs,
892906
redis_url,
893907
redis_pool_size,
908+
db_pool_size,
894909
relay_url,
895910
pairing_relay_url,
896911
max_connections,
@@ -958,6 +973,7 @@ mod tests {
958973
assert!(!config.database_url.is_empty());
959974
assert!(!config.redis_url.is_empty());
960975
assert_eq!(config.redis_pool_size, 16);
976+
assert_eq!(config.db_pool_size, 50);
961977
assert!(config.max_connections > 0);
962978
assert!(config.send_buffer_size > 0);
963979
assert_eq!(config.max_frame_bytes, DEFAULT_MAX_FRAME_BYTES);
@@ -1025,6 +1041,31 @@ mod tests {
10251041
assert_eq!(junk, 16, "unparsable value must fall back to the default");
10261042
}
10271043

1044+
#[test]
1045+
fn db_pool_size_env_override_and_invalid_fallback() {
1046+
let _guard = ENV_MUTEX.lock().unwrap();
1047+
let previous = std::env::var_os("BUZZ_DB_POOL_SIZE");
1048+
1049+
std::env::set_var("BUZZ_DB_POOL_SIZE", "80");
1050+
let overridden = Config::from_env().expect("config").db_pool_size;
1051+
1052+
std::env::set_var("BUZZ_DB_POOL_SIZE", "0");
1053+
let zero = Config::from_env().expect("config").db_pool_size;
1054+
1055+
std::env::set_var("BUZZ_DB_POOL_SIZE", "not-a-number");
1056+
let junk = Config::from_env().expect("config").db_pool_size;
1057+
1058+
if let Some(value) = previous {
1059+
std::env::set_var("BUZZ_DB_POOL_SIZE", value);
1060+
} else {
1061+
std::env::remove_var("BUZZ_DB_POOL_SIZE");
1062+
}
1063+
1064+
assert_eq!(overridden, 80);
1065+
assert_eq!(zero, 50, "zero must fall back to the default");
1066+
assert_eq!(junk, 50, "unparsable value must fall back to the default");
1067+
}
1068+
10281069
#[test]
10291070
fn read_database_url_unset_or_blank_is_none() {
10301071
let _guard = ENV_MUTEX.lock().unwrap();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ async fn main() -> anyhow::Result<()> {
159159
database_url: config.database_url.clone(),
160160
read_database_url: config.read_database_url.clone(),
161161
replica_head_max_age_secs: config.replica_head_max_age_secs,
162+
max_connections: config.db_pool_size,
162163
..DbConfig::default()
163164
};
164165
let db = Db::new(&db_config).await.map_err(|e| {

0 commit comments

Comments
 (0)