Select a server to run an operation on this client. :Parameters: - `server_selector`: The server selector to use if the session is not pinned and no address is given. - `session`: The ClientSession for the next operation, or None. May be pinned to
(
self,
server_selector: Callable[[Selection], Selection],
session: Optional[ClientSession],
operation: str,
address: Optional[_Address] = None,
deprioritized_servers: Optional[list[Server]] = None,
operation_id: Optional[int] = None,
)
| 1816 | yield conn |
| 1817 | |
| 1818 | def _select_server( |
| 1819 | self, |
| 1820 | server_selector: Callable[[Selection], Selection], |
| 1821 | session: Optional[ClientSession], |
| 1822 | operation: str, |
| 1823 | address: Optional[_Address] = None, |
| 1824 | deprioritized_servers: Optional[list[Server]] = None, |
| 1825 | operation_id: Optional[int] = None, |
| 1826 | ) -> Server: |
| 1827 | """Select a server to run an operation on this client. |
| 1828 | |
| 1829 | :Parameters: |
| 1830 | - `server_selector`: The server selector to use if the session is |
| 1831 | not pinned and no address is given. |
| 1832 | - `session`: The ClientSession for the next operation, or None. May |
| 1833 | be pinned to a mongos server address. |
| 1834 | - `address` (optional): Address when sending a message |
| 1835 | to a specific server, used for getMore. |
| 1836 | """ |
| 1837 | try: |
| 1838 | topology = self._get_topology() |
| 1839 | if session and not session.in_transaction: |
| 1840 | session._transaction.reset() |
| 1841 | if not address and session: |
| 1842 | address = session._pinned_address |
| 1843 | if address: |
| 1844 | # We're running a getMore or this session is pinned to a mongos. |
| 1845 | server = topology.select_server_by_address( |
| 1846 | address, operation, operation_id=operation_id |
| 1847 | ) |
| 1848 | if not server: |
| 1849 | raise AutoReconnect("server %s:%s no longer available" % address) # noqa: UP031 |
| 1850 | else: |
| 1851 | server = topology.select_server( |
| 1852 | server_selector, |
| 1853 | operation, |
| 1854 | deprioritized_servers=deprioritized_servers, |
| 1855 | operation_id=operation_id, |
| 1856 | ) |
| 1857 | return server |
| 1858 | except PyMongoError as exc: |
| 1859 | # Server selection errors in a transaction are transient. |
| 1860 | if session and session.in_transaction: |
| 1861 | exc._add_error_label("TransientTransactionError") |
| 1862 | session._unpin() |
| 1863 | raise |
| 1864 | |
| 1865 | def _conn_for_writes( |
| 1866 | self, session: Optional[ClientSession], operation: str |
no test coverage detected