Skip to content

Commit

Permalink
PYTHON-4473 Optimize find/aggregate/command by avoiding duplicate cal…
Browse files Browse the repository at this point in the history
…ls to _get_topology (#1652) (#1654)
  • Loading branch information
ShaneHarvey authored Jun 1, 2024
1 parent d4592b6 commit fa80968
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pymongo/mongo_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)

Expand Down

0 comments on commit fa80968

Please sign in to comment.