| 695 | |
| 696 | |
| 697 | class Pool: |
| 698 | def __init__( |
| 699 | self, |
| 700 | address: _Address, |
| 701 | options: PoolOptions, |
| 702 | is_sdam: bool = False, |
| 703 | client_id: Optional[ObjectId] = None, |
| 704 | ): |
| 705 | """ |
| 706 | :param address: a (hostname, port) tuple |
| 707 | :param options: a PoolOptions instance |
| 708 | :param is_sdam: whether to call hello for each new Connection |
| 709 | """ |
| 710 | if options.pause_enabled: |
| 711 | self.state = PoolState.PAUSED |
| 712 | else: |
| 713 | self.state = PoolState.READY |
| 714 | # Check a socket's health with socket_closed() every once in a while. |
| 715 | # Can override for testing: 0 to always check, None to never check. |
| 716 | self._check_interval_seconds = 1 |
| 717 | # LIFO pool. Sockets are ordered on idle time. Sockets claimed |
| 718 | # and returned to pool from the left side. Stale sockets removed |
| 719 | # from the right side. |
| 720 | self.conns: collections.deque[Connection] = collections.deque() |
| 721 | self.active_contexts: set[_CancellationContext] = set() |
| 722 | self.lock = _create_lock() |
| 723 | self._max_connecting_cond = _create_condition(self.lock) |
| 724 | self.active_sockets = 0 |
| 725 | # Monotonically increasing connection ID required for CMAP Events. |
| 726 | self.next_connection_id = 1 |
| 727 | # Track whether the sockets in this pool are writeable or not. |
| 728 | self.is_writable: Optional[bool] = None |
| 729 | |
| 730 | # Keep track of resets, so we notice sockets created before the most |
| 731 | # recent reset and close them. |
| 732 | # self.generation = 0 |
| 733 | self.gen = _PoolGeneration() |
| 734 | self.pid = os.getpid() |
| 735 | self.address = address |
| 736 | self.opts = options |
| 737 | self.is_sdam = is_sdam |
| 738 | # Don't publish events or logs in Monitor pools. |
| 739 | self.enabled_for_cmap = ( |
| 740 | not self.is_sdam |
| 741 | and self.opts._event_listeners is not None |
| 742 | and self.opts._event_listeners.enabled_for_cmap |
| 743 | ) |
| 744 | self.enabled_for_logging = not self.is_sdam |
| 745 | |
| 746 | # The first portion of the wait queue. |
| 747 | # Enforces: maxPoolSize |
| 748 | # Also used for: clearing the wait queue |
| 749 | self.size_cond = _create_condition(self.lock) |
| 750 | self.requests = 0 |
| 751 | self.max_pool_size = self.opts.max_pool_size |
| 752 | if not self.max_pool_size: |
| 753 | self.max_pool_size = float("inf") |
| 754 | # The second portion of the wait queue. |
no outgoing calls