|
1 | 1 | import asyncio |
| 2 | +from typing import Any, Callable, Coroutine |
| 3 | + |
2 | 4 | from sqlalchemy.ext.asyncio import ( |
3 | 5 | async_sessionmaker, |
4 | 6 | AsyncEngine, |
5 | 7 | AsyncSession, |
6 | 8 | ) |
7 | 9 |
|
| 10 | +EngineCreatorFunc = Callable[[str], AsyncEngine] |
| 11 | +SessionMakerCreatorFunc = Callable[ |
| 12 | + [AsyncEngine], async_sessionmaker[AsyncSession] |
| 13 | +] |
| 14 | +AsyncFunc = Callable[["DBConnect"], Coroutine[Any, Any, None]] |
| 15 | + |
8 | 16 |
|
9 | 17 | class DBConnect: |
10 | 18 | """stores the database connection parameters""" |
11 | 19 |
|
12 | 20 | def __init__(self) -> None: |
13 | | - self.engine: AsyncEngine | None = None |
14 | | - self.session_maker: async_sessionmaker[AsyncSession] | None = None |
| 21 | + self.host: str | None = None |
| 22 | + self._engine: AsyncEngine | None = None |
| 23 | + self._session_maker: async_sessionmaker[AsyncSession] | None = None |
| 24 | + |
| 25 | + self.engine_creator: EngineCreatorFunc | None = None |
| 26 | + self.session_maker_creator: SessionMakerCreatorFunc | None = None |
| 27 | + self.before_create_session_handler: AsyncFunc | None = None |
15 | 28 | self._lock = asyncio.Lock() |
16 | 29 |
|
17 | | - async def close(self) -> None: |
18 | | - if self.engine: |
19 | | - await self.engine.dispose() |
20 | | - self.engine = None |
| 30 | + async def connect(self, host: str) -> None: |
| 31 | + """initiates engine and session maker""" |
| 32 | + assert host |
| 33 | + async with self._lock: |
| 34 | + await self._connect(host) |
21 | 35 |
|
| 36 | + async def change_host(self, host: str) -> None: |
| 37 | + """Renews the connection if a host needs to be changed""" |
| 38 | + assert host |
| 39 | + async with self._lock: |
| 40 | + if host != self.host: |
| 41 | + await self._connect(host) |
22 | 42 |
|
23 | | -db_connect = DBConnect() |
| 43 | + async def create_session(self) -> AsyncSession: |
| 44 | + """Creates a new session""" |
| 45 | + if self.before_create_session_handler: |
| 46 | + await self.before_create_session_handler(self) |
| 47 | + maker = await self.get_session_maker() |
| 48 | + return maker() |
24 | 49 |
|
| 50 | + async def get_session_maker(self) -> async_sessionmaker[AsyncSession]: |
| 51 | + """Gets the session maker""" |
| 52 | + if not self._session_maker: |
| 53 | + assert self.host |
| 54 | + await self.connect(self.host) |
| 55 | + |
| 56 | + assert self._session_maker |
| 57 | + return self._session_maker |
| 58 | + |
| 59 | + async def close(self) -> None: |
| 60 | + if self._engine: |
| 61 | + await self._engine.dispose() |
| 62 | + self._engine = None |
25 | 63 |
|
26 | | -def get_session_maker() -> async_sessionmaker[AsyncSession]: |
27 | | - """Gets the session maker""" |
28 | | - assert db_connect.session_maker |
29 | | - return db_connect.session_maker |
| 64 | + async def _connect(self, host: str) -> None: |
| 65 | + self.host = host |
| 66 | + await self.close() |
| 67 | + assert self.engine_creator |
| 68 | + self._engine = self.engine_creator(host) |
| 69 | + assert self.session_maker_creator |
| 70 | + self._session_maker = self.session_maker_creator(self._engine) |
30 | 71 |
|
31 | 72 |
|
32 | | -def create_session() -> AsyncSession: |
33 | | - """Creates a new session""" |
34 | | - assert db_connect.session_maker |
35 | | - return db_connect.session_maker() |
| 73 | +master_connect = DBConnect() |
| 74 | +replica_connect = DBConnect() |
0 commit comments