MCPcopy Index your code
hub / github.com/mongodb/mongo-python-driver / _select_server

Method _select_server

pymongo/synchronous/mongo_client.py:1818–1863  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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

Callers 4

_conn_for_writesMethod · 0.95
_conn_for_readsMethod · 0.95
_run_operationMethod · 0.95
_get_serverMethod · 0.45

Calls 7

_get_topologyMethod · 0.95
AutoReconnectClass · 0.90
_add_error_labelMethod · 0.80
resetMethod · 0.45
select_serverMethod · 0.45
_unpinMethod · 0.45

Tested by

no test coverage detected