| 1167 | |
| 1168 | |
| 1169 | class PoolAcquireContext: |
| 1170 | |
| 1171 | __slots__ = ('timeout', 'connection', 'done', 'pool') |
| 1172 | |
| 1173 | def __init__(self, pool: Pool, timeout: Optional[float]) -> None: |
| 1174 | self.pool = pool |
| 1175 | self.timeout = timeout |
| 1176 | self.connection = None |
| 1177 | self.done = False |
| 1178 | |
| 1179 | async def __aenter__(self): |
| 1180 | if self.connection is not None or self.done: |
| 1181 | raise exceptions.InterfaceError('a connection is already acquired') |
| 1182 | self.connection = await self.pool._acquire(self.timeout) |
| 1183 | return self.connection |
| 1184 | |
| 1185 | async def __aexit__( |
| 1186 | self, |
| 1187 | exc_type: Optional[Type[BaseException]] = None, |
| 1188 | exc_val: Optional[BaseException] = None, |
| 1189 | exc_tb: Optional[TracebackType] = None, |
| 1190 | ) -> None: |
| 1191 | self.done = True |
| 1192 | con = self.connection |
| 1193 | self.connection = None |
| 1194 | await self.pool.release(con) |
| 1195 | |
| 1196 | def __await__(self): |
| 1197 | self.done = True |
| 1198 | return self.pool._acquire(self.timeout).__await__() |
| 1199 | |
| 1200 | |
| 1201 | def create_pool(dsn=None, *, |