@@ -13,8 +13,11 @@ use scheduled_thread_pool::ScheduledThreadPool;
1313// The db, oauth, and git2 types don't implement debug.
1414#[ allow( missing_debug_implementations) ]
1515pub struct App {
16- /// The database connection pool
17- pub diesel_database : db:: DieselPool ,
16+ /// The primary database connection pool
17+ pub primary_database : db:: DieselPool ,
18+
19+ /// The read-only replica database connection pool
20+ pub read_only_replica_database : Option < db:: DieselPool > ,
1821
1922 /// The GitHub OAuth2 configuration
2023 pub github : BasicClient ,
@@ -78,29 +81,53 @@ impl App {
7881 _ => 1 ,
7982 } ;
8083
84+ // Used as the connection and statement timeout value for the database pool(s)
8185 let db_connection_timeout = match ( dotenv:: var ( "DB_TIMEOUT" ) , config. env ) {
8286 ( Ok ( num) , _) => num. parse ( ) . expect ( "couldn't parse DB_TIMEOUT" ) ,
8387 ( _, Env :: Production ) => 10 ,
8488 ( _, Env :: Test ) => 1 ,
8589 _ => 30 ,
8690 } ;
91+
92+ // Determine if the primary pool is also read-only
8793 let read_only_mode = dotenv:: var ( "READ_ONLY_MODE" ) . is_ok ( ) ;
88- let connection_config = db:: ConnectionConfig {
94+ let primary_db_connection_config = db:: ConnectionConfig {
8995 statement_timeout : db_connection_timeout,
9096 read_only : read_only_mode,
9197 } ;
9298
9399 let thread_pool = Arc :: new ( ScheduledThreadPool :: new ( db_helper_threads) ) ;
94100
95- let diesel_db_config = r2d2:: Pool :: builder ( )
101+ let primary_db_config = r2d2:: Pool :: builder ( )
96102 . max_size ( db_pool_size)
97103 . min_idle ( db_min_idle)
98104 . connection_timeout ( Duration :: from_secs ( db_connection_timeout) )
99- . connection_customizer ( Box :: new ( connection_config) )
100- . thread_pool ( thread_pool) ;
105+ . connection_customizer ( Box :: new ( primary_db_connection_config) )
106+ . thread_pool ( thread_pool. clone ( ) ) ;
107+
108+ let primary_database = db:: diesel_pool ( & config. db_url , config. env , primary_db_config) ;
109+
110+ let read_only_replica_database = if let Some ( url) = & config. replica_db_url {
111+ let replica_db_connection_config = db:: ConnectionConfig {
112+ statement_timeout : db_connection_timeout,
113+ read_only : true ,
114+ } ;
115+
116+ let replica_db_config = r2d2:: Pool :: builder ( )
117+ . max_size ( db_pool_size)
118+ . min_idle ( db_min_idle)
119+ . connection_timeout ( Duration :: from_secs ( db_connection_timeout) )
120+ . connection_customizer ( Box :: new ( replica_db_connection_config) )
121+ . thread_pool ( thread_pool) ;
122+
123+ Some ( db:: diesel_pool ( & url, config. env , replica_db_config) )
124+ } else {
125+ None
126+ } ;
101127
102128 App {
103- diesel_database : db:: diesel_pool ( & config. db_url , config. env , diesel_db_config) ,
129+ primary_database,
130+ read_only_replica_database,
104131 github,
105132 session_key : config. session_key . clone ( ) ,
106133 git_repo_checkout : config. git_repo_checkout . clone ( ) ,
0 commit comments