File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1414
1515import psycopg2
1616import psycopg2 .extras
17+ from contextlib import contextmanager
18+ from psycopg2 .pool import ThreadedConnectionPool
1719
1820logger = logging .getLogger ("atomicloop.storage" )
1921
2022
2123class RunStorage :
2224
2325 def __init__ (self , db_path : str = "./atomicloop.db" ):
24- self ._url = os .environ .get ("DATABASE_URL" ) or db_path
26+ url = os .environ .get ("DATABASE_URL" ) or db_path
27+ self ._pool = ThreadedConnectionPool (minconn = 1 , maxconn = 10 , dsn = url )
2528
29+ @contextmanager
2630 def _get_conn (self ):
27- return psycopg2 .connect (self ._url )
31+ conn = self ._pool .getconn ()
32+ try :
33+ yield conn
34+ except Exception :
35+ conn .rollback ()
36+ raise
37+ finally :
38+ self ._pool .putconn (conn )
2839
2940 # ── Write ──────────────────────────────────────────────────────────────────
3041
You can’t perform that action at this time.
0 commit comments