From 2210f74002663bee1c7bebc4953cd363824e02d5 Mon Sep 17 00:00:00 2001 From: Shane Harvey Date: Fri, 31 May 2024 14:42:36 -0700 Subject: [PATCH] PYTHON-4473 Optimize find/aggregate/command by avoiding duplicate calls to _get_topology (#1652) --- pymongo/mongo_client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pymongo/mongo_client.py b/pymongo/mongo_client.py index f2076b0877..b0824acd44 100644 --- a/pymongo/mongo_client.py +++ b/pymongo/mongo_client.py @@ -1344,8 +1344,9 @@ def _conn_from_server( # always send primaryPreferred when directly connected to a repl set # member. # Thread safe: if the type is single it cannot change. - topology = self._get_topology() - single = topology.description.topology_type == TOPOLOGY_TYPE.Single + # NOTE: We already opened the Topology when selecting a server so there's no need + # to call _get_topology() again. + single = self._topology.description.topology_type == TOPOLOGY_TYPE.Single with self._checkout(server, session) as conn: if single: @@ -1365,7 +1366,6 @@ def _conn_for_reads( operation: str, ) -> ContextManager[tuple[Connection, _ServerMode]]: assert read_preference is not None, "read_preference must not be None" - _ = self._get_topology() server = self._select_server(read_preference, session, operation) return self._conn_from_server(read_preference, server, session)