Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PYTHON-4473 Optimize find/aggregate/command by avoiding duplicate calls to _get_topology #1652

Merged
merged 1 commit into from
May 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading