Retrieve a random session from the pool. This method first ensures the session pool is at its maximum capacity. If the random session is not usable, retired sessions are removed and a new session is created and returned. Returns: The session object.
(self)
| 178 | |
| 179 | @ensure_context |
| 180 | async def get_session(self) -> Session: |
| 181 | """Retrieve a random session from the pool. |
| 182 | |
| 183 | This method first ensures the session pool is at its maximum capacity. If the random session is not usable, |
| 184 | retired sessions are removed and a new session is created and returned. |
| 185 | |
| 186 | Returns: |
| 187 | The session object. |
| 188 | """ |
| 189 | await self._fill_sessions_to_max() |
| 190 | session = self._get_random_session() |
| 191 | |
| 192 | if session.is_usable: |
| 193 | return session |
| 194 | |
| 195 | # If the random session is not usable, clean up and create a new session |
| 196 | self._remove_retired_sessions() |
| 197 | return await self._create_new_session() |
| 198 | |
| 199 | @ensure_context |
| 200 | async def get_session_by_id(self, session_id: str) -> Session | None: |