Skip to content

PYTHON-5672 Refactor connection checkout to reduce layers#2893

Draft
blink1073 wants to merge 2 commits into
mongodb:masterfrom
blink1073:PYTHON-5672
Draft

PYTHON-5672 Refactor connection checkout to reduce layers#2893
blink1073 wants to merge 2 commits into
mongodb:masterfrom
blink1073:PYTHON-5672

Conversation

@blink1073

Copy link
Copy Markdown
Member

PYTHON-5672

Changes in this PR

Flatten the connection checkout hot path by replacing @asynccontextmanager generator-based context managers with class-based ones, and eliminate _MongoClientErrorHandler by absorbing it into the new _ClientCheckout class. Reduces per-checkout overhead by ~3 generator frames (~400–500 ns each).

Test Plan

  • just typing passes (mypy + pyright)
  • just lint-manual passes (all pre-commit hooks)
  • Existing CMAP and unified format spec tests cover the checkout/checkin contract and SDAM error handling ordering

Checklist

Checklist for Author

  • Did you update the changelog (if necessary)?
  • Is there test coverage?
  • Is any followup work tracked in a JIRA ticket? If so, add link(s).

Checklist for Reviewer

  • Does the title of the PR reference a JIRA Ticket?
  • Do you fully understand the implementation? (Would you be comfortable explaining how this code works to someone else?)
  • Is all relevant documentation (README or docstring) updated?

Replace the three @asynccontextmanager layers on the connection checkout
hot path with class-based async context managers, and eliminate
_MongoClientErrorHandler by absorbing it into _ClientCheckout.

- _PoolCheckout replaces Pool.checkout() generator CM
- _ClientCheckout replaces _checkout() generator CM and absorbs all of
  _MongoClientErrorHandler (contribute_socket, handle, SDAM error logic)
- _ClientReadCheckout extends _ClientCheckout to apply single-topology
  read preference adjustment (formerly _conn_from_server())
- active_contexts.add() consolidated into _get_conn(), avoiding a
  separate lock acquisition on the hot path; deduped so new connections
  (already tracked by connect()) are not double-added
- Connection leak fixed: self._conn assigned before event publishing so
  checkin runs if a CMAP listener raises in __aenter__
- _ClientCheckout.for_existing_conn() classmethod handles the
  _run_operation() getMore path that needs SDAM handling around an
  already-checked-out connection
…onnectionRetryable

Eliminates per-instance __dict__ allocation and defers the _deprioritized_servers
list creation until it is actually needed (only on sharded retry paths).
@codecov-commenter

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants