@@ -51,6 +51,22 @@ async fn connect_audit_pool(config: &DbConfig) -> anyhow::Result<sqlx::PgPool> {
5151 . map_err ( Into :: into)
5252}
5353
54+ /// The search pool. Search is lag-tolerant, so it prefers the read replica
55+ /// when one is configured. Like the main read pool it is lazy, so a reader
56+ /// that is down at boot fails searches, not startup, and it waits no longer
57+ /// than the writer pool for a connection, so a dead reader fails each search
58+ /// within that bound instead of sqlx's 30s default.
59+ fn connect_search_pool ( config : & DbConfig ) -> anyhow:: Result < sqlx:: PgPool > {
60+ let url = config
61+ . read_database_url
62+ . as_deref ( )
63+ . unwrap_or ( & config. database_url ) ;
64+ sqlx:: postgres:: PgPoolOptions :: new ( )
65+ . acquire_timeout ( std:: time:: Duration :: from_secs ( config. acquire_timeout_secs ) )
66+ . connect_lazy ( url)
67+ . map_err ( |e| anyhow:: anyhow!( "Search DB URL is invalid: {e}" ) )
68+ }
69+
5470fn relay_keypair_from_config ( relay_private_key : Option < & str > ) -> anyhow:: Result < nostr:: Keys > {
5571 let hex = relay_private_key. ok_or_else ( || {
5672 anyhow:: anyhow!(
@@ -834,18 +850,8 @@ async fn run_relay_main(boot: BootTracker) -> anyhow::Result<()> {
834850 // Postgres FTS: the searchable row IS the persisted event row (its
835851 // `tsvector` column is populated by the `insert_event` write), so there is
836852 // no external collection to provision — the search service just queries the
837- // same Postgres over its own pool. Search is lag-tolerant, so it prefers
838- // the read replica when one is configured.
839- let search_db_url = config
840- . read_database_url
841- . as_deref ( )
842- . unwrap_or ( & config. database_url ) ;
843- let step = StepTimer :: start ( StartupStep :: SearchConnect ) ;
844- let search_pool = sqlx:: postgres:: PgPoolOptions :: new ( )
845- . connect ( search_db_url)
846- . await
847- . map_err ( |e| anyhow:: anyhow!( "Search DB connection failed: {e}" ) ) ?;
848- step. finish ( ) ;
853+ // same Postgres over its own pool (see `connect_search_pool`).
854+ let search_pool = connect_search_pool ( & db_config) ?;
849855 let search_metrics_pool = search_pool. clone ( ) ;
850856 let search = SearchService :: new ( search_pool) ;
851857 info ! (
@@ -3222,9 +3228,9 @@ mod tests {
32223228
32233229 use super :: {
32243230 apply_activity_refresh, apply_stock_refresh, buzz_auto_migrate_enabled, connect_audit_pool,
3225- counted_partition_audit , dropped_in_memory_keys , emit_cached_fleet_usage_metrics ,
3226- idle_timeout_secs , jwks_next_retry_after_failed_refresh , nip_fi_jwks_refresh_loop ,
3227- record_legacy_usage_collection, refresh_due_fleet_usage,
3231+ connect_search_pool , counted_partition_audit , dropped_in_memory_keys ,
3232+ emit_cached_fleet_usage_metrics , idle_timeout_secs , jwks_next_retry_after_failed_refresh ,
3233+ nip_fi_jwks_refresh_loop , record_legacy_usage_collection, refresh_due_fleet_usage,
32283234 refresh_legacy_active_gauge_recency, relay_keypair_from_config,
32293235 run_jwks_refresh_supervisor, run_periodic_until_cancelled, startup_partition_audit,
32303236 EmissionScope , FleetUsageSchedule , InMemoryMetricKey , PartitionAuditSchedule ,
@@ -3237,6 +3243,22 @@ mod tests {
32373243 registry:: { GenerationalAtomicStorage , Registry } ,
32383244 } ;
32393245
3246+ #[ tokio:: test]
3247+ async fn search_pool_is_lazy_prefers_the_reader_and_bounds_acquire ( ) {
3248+ // Unroutable per RFC 5737 TEST-NET-1: construction must not dial it.
3249+ let config = DbConfig {
3250+ read_database_url : Some ( "postgres://u:[email protected] :5432/none" . to_owned ( ) ) , // sadscan:disable np.postgres.1 3251+ ..DbConfig :: default ( )
3252+ } ;
3253+ let pool = connect_search_pool ( & config) . expect ( "lazy construction must not dial" ) ;
3254+ assert_eq ! ( pool. connect_options( ) . get_host( ) , "192.0.2.1" ) ;
3255+ assert_eq ! (
3256+ pool. options( ) . get_acquire_timeout( ) ,
3257+ Duration :: from_secs( config. acquire_timeout_secs) ,
3258+ "a dead reader must fail a search within the writer's acquire bound"
3259+ ) ;
3260+ }
3261+
32403262 #[ tokio:: test( start_paused = true ) ]
32413263 async fn periodic_loop_exits_immediately_on_cancellation ( ) {
32423264 let cancel = CancellationToken :: new ( ) ;
0 commit comments