File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import os
22import logging
3+ from functools import lru_cache
34from itertools import chain
45from typing import Union , Sequence
56from sqlalchemy .engine import Engine , URL
2829# --- Database connection functions ---
2930
3031
31- _engine_cache : dict [str , Engine ] = {}
32+ @lru_cache
33+ def _cached_engine (url : URL ) -> Engine :
34+ # URL objects hash/compare on their real fields (including password), so
35+ # caching on the URL itself is safe. str(URL) masks the password as
36+ # "***" for safe logging — passing that to create_engine() instead would
37+ # make every connection attempt authenticate with the literal "***".
38+ return create_engine (url )
3239
3340
3441def get_engine () -> Engine :
@@ -41,13 +48,7 @@ def get_engine() -> Engine:
4148 the same process (e.g. browser vs. browser-csrf DBs) still get one
4249 engine per database instead of sharing a mismatched pool.
4350 """
44- url = get_connection_url ()
45- key = str (url )
46- engine = _engine_cache .get (key )
47- if engine is None :
48- engine = create_engine (url )
49- _engine_cache [key ] = engine
50- return engine
51+ return _cached_engine (get_connection_url ())
5152
5253
5354def ensure_database_exists (url : URL ) -> None :
You can’t perform that action at this time.
0 commit comments