1111 BROOD_DB_URI_READ_ONLY ,
1212 BROOD_POOL_SIZE ,
1313 BROOD_DB_STATEMENT_TIMEOUT_MILLIS ,
14+ BROOD_DB_POOL_RECYCLE_SECONDS ,
1415)
1516
1617
17- def create_brood_engine (url : Optional [str ], pool_size : int , statement_timeout : int ):
18+ def create_brood_engine (url : Optional [str ], pool_size : int , statement_timeout : int , pool_recycle : int = BROOD_DB_POOL_RECYCLE_SECONDS ):
1819 # Pooling: https://docs.sqlalchemy.org/en/14/core/pooling.html#sqlalchemy.pool.QueuePool
1920 # Statement timeout: https://stackoverflow.com/a/44936982
2021 return create_engine (
2122 url = url ,
2223 pool_size = pool_size ,
24+ pool_recycle = pool_recycle ,
2325 connect_args = {"options" : f"-c statement_timeout={ statement_timeout } " },
2426 )
2527
26-
2728engine = create_brood_engine (
2829 url = BROOD_DB_URI ,
2930 pool_size = BROOD_POOL_SIZE ,
3031 statement_timeout = BROOD_DB_STATEMENT_TIMEOUT_MILLIS ,
32+ pool_recycle = BROOD_DB_POOL_RECYCLE_SECONDS ,
3133)
3234SessionLocal = sessionmaker (autocommit = False , autoflush = False , bind = engine )
3335
@@ -52,6 +54,7 @@ def yield_db_session_from_env() -> Session:
5254 url = BROOD_DB_URI_READ_ONLY ,
5355 pool_size = BROOD_POOL_SIZE ,
5456 statement_timeout = BROOD_DB_STATEMENT_TIMEOUT_MILLIS ,
57+ pool_recycle = BROOD_DB_POOL_RECYCLE_SECONDS ,
5558)
5659RO_SessionLocal = sessionmaker (autocommit = False , autoflush = False , bind = RO_engine )
5760
0 commit comments