Verify the state of the connections currently in the pool. Test each connection: if it works return it to the pool, otherwise dispose of it and create a new one.
(self)
| 511 | self.run_task(AddConnection(self)) |
| 512 | |
| 513 | def check(self) -> None: |
| 514 | """Verify the state of the connections currently in the pool. |
| 515 | |
| 516 | Test each connection: if it works return it to the pool, otherwise |
| 517 | dispose of it and create a new one. |
| 518 | """ |
| 519 | with self._lock: |
| 520 | conns = list(self._pool) |
| 521 | self._pool.clear() |
| 522 | |
| 523 | # Give a chance to the pool to grow if it has no connection. |
| 524 | # In case there are enough connection, or the pool is already |
| 525 | # growing, this is a no-op. |
| 526 | self._maybe_grow_pool() |
| 527 | |
| 528 | while conns: |
| 529 | conn = conns.pop() |
| 530 | |
| 531 | # Check for expired connections |
| 532 | if conn._expire_at <= monotonic(): |
| 533 | logger.info("discarding expired connection %s", conn) |
| 534 | self._close_connection(conn) |
| 535 | self.run_task(AddConnection(self)) |
| 536 | continue |
| 537 | |
| 538 | # Check for broken connections |
| 539 | try: |
| 540 | self.check_connection(conn) |
| 541 | except CLIENT_EXCEPTIONS: |
| 542 | self._stats[self._CONNECTIONS_LOST] += 1 |
| 543 | logger.warning("discarding broken connection: %s", conn) |
| 544 | self.run_task(AddConnection(self)) |
| 545 | else: |
| 546 | self._add_to_pool(conn) |
| 547 | |
| 548 | @staticmethod |
| 549 | def check_connection(conn: CT) -> None: |