Add a connection to the pool. The connection can be a fresh one or one already used in the pool. If a client is already waiting for a connection pass it on, otherwise put it back into the pool
(self, conn: CT)
| 734 | self._add_to_pool(conn) |
| 735 | |
| 736 | def _add_to_pool(self, conn: CT) -> None: |
| 737 | """ |
| 738 | Add a connection to the pool. |
| 739 | |
| 740 | The connection can be a fresh one or one already used in the pool. |
| 741 | |
| 742 | If a client is already waiting for a connection pass it on, otherwise |
| 743 | put it back into the pool |
| 744 | """ |
| 745 | # Remove the pool reference from the connection before returning it |
| 746 | # to the state, to avoid to create a reference loop. |
| 747 | # Also disable the warning for open connection in conn.__del__ |
| 748 | conn._pool = None |
| 749 | |
| 750 | # Early bailout in case the pool is closed. Don't add anything to the |
| 751 | # state. There is still a remote chance that the pool will be closed |
| 752 | # between here and entering the lock. Therefore we will make another |
| 753 | # check later. |
| 754 | if self._closed: |
| 755 | self._close_connection(conn) |
| 756 | return |
| 757 | |
| 758 | # Critical section: if there is a client waiting give it the connection |
| 759 | # otherwise put it back into the pool. |
| 760 | with self._lock: |
| 761 | # Check if the pool was closed by the time we arrived here. It is |
| 762 | # unlikely but it doesn't seem impossible, if the worker was adding |
| 763 | # this connection while the main process is closing the pool. |
| 764 | # Now that we are in the critical section we know for real. |
| 765 | if self._closed: |
| 766 | self._close_connection(conn) |
| 767 | return |
| 768 | |
| 769 | while self._waiting: |
| 770 | # If there is a client waiting (which is still waiting and |
| 771 | # hasn't timed out), give it the connection and notify it. |
| 772 | |
| 773 | if self._waiting.popleft().set(conn): |
| 774 | break |
| 775 | else: |
| 776 | # No client waiting for a connection: put it back into the pool |
| 777 | self._pool.append(conn) |
| 778 | # If we have been asked to wait for pool init, notify the |
| 779 | # waiter if the pool is full. |
| 780 | if self._pool_full_event and len(self._pool) >= self._min_size: |
| 781 | self._pool_full_event.set() |
| 782 | |
| 783 | def _reset_connection(self, conn: CT) -> None: |
| 784 | """ |
no test coverage detected