| 422 | return conn |
| 423 | |
| 424 | def borrow_connection(self, timeout): |
| 425 | conn = self._get_connection() |
| 426 | if conn.orphaned_threshold_reached: |
| 427 | with self._lock: |
| 428 | if not self._is_replacing: |
| 429 | self._is_replacing = True |
| 430 | self._session.submit(self._replace, conn) |
| 431 | log.debug( |
| 432 | "Connection to host %s reached orphaned stream limit, replacing...", |
| 433 | self.host |
| 434 | ) |
| 435 | |
| 436 | start = time.time() |
| 437 | remaining = timeout |
| 438 | while True: |
| 439 | with conn.lock: |
| 440 | if not (conn.orphaned_threshold_reached and conn.is_closed) and conn.in_flight < conn.max_request_id: |
| 441 | conn.in_flight += 1 |
| 442 | return conn, conn.get_request_id() |
| 443 | if timeout is not None: |
| 444 | remaining = timeout - time.time() + start |
| 445 | if remaining < 0: |
| 446 | break |
| 447 | with self._stream_available_condition: |
| 448 | if conn.orphaned_threshold_reached and conn.is_closed: |
| 449 | conn = self._get_connection() |
| 450 | else: |
| 451 | self._stream_available_condition.wait(remaining) |
| 452 | |
| 453 | raise NoConnectionsAvailable("All request IDs are currently in use") |
| 454 | |
| 455 | def return_connection(self, connection, stream_was_orphaned=False): |
| 456 | if not stream_was_orphaned: |