Obtain a connection from the pool. You should preferably use `connection()`. Use this function only if it is not possible to use the connection as context manager. After using this function you *must* call a corresponding `putconn()`: failing to do so will deplete t
(self, timeout: float | None = None)
| 192 | self._stats[self._USAGE_MS] += int(1000.0 * (t1 - t0)) |
| 193 | |
| 194 | def getconn(self, timeout: float | None = None) -> CT: |
| 195 | """Obtain a connection from the pool. |
| 196 | |
| 197 | You should preferably use `connection()`. Use this function only if |
| 198 | it is not possible to use the connection as context manager. |
| 199 | |
| 200 | After using this function you *must* call a corresponding `putconn()`: |
| 201 | failing to do so will deplete the pool. A depleted pool is a sad pool: |
| 202 | you don't want a depleted pool. |
| 203 | """ |
| 204 | if timeout is None: |
| 205 | timeout = self.timeout |
| 206 | deadline = monotonic() + timeout |
| 207 | |
| 208 | logger.info("connection requested from %r", self.name) |
| 209 | self._stats[self._REQUESTS_NUM] += 1 |
| 210 | |
| 211 | self._check_open_getconn() |
| 212 | |
| 213 | try: |
| 214 | return self._getconn_with_check_loop(deadline) |
| 215 | # Re-raise the timeout exception presenting the user the global |
| 216 | # timeout, not the per-attempt one. |
| 217 | except PoolTimeout: |
| 218 | raise PoolTimeout( |
| 219 | f"couldn't get a connection after {timeout:.2f} sec" |
| 220 | ) from None |
| 221 | |
| 222 | def _getconn_with_check_loop(self, deadline: float) -> CT: |
| 223 | attempt: AttemptWithBackoff | None = None |