@@ -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 ( ) ;
0 commit comments