PYTHON-5672 Refactor connection checkout to reduce layers#2893
Draft
blink1073 wants to merge 2 commits into
Draft
PYTHON-5672 Refactor connection checkout to reduce layers#2893blink1073 wants to merge 2 commits into
blink1073 wants to merge 2 commits into
Conversation
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 Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PYTHON-5672
Changes in this PR
Flatten the connection checkout hot path by replacing
@asynccontextmanagergenerator-based context managers with class-based ones, and eliminate_MongoClientErrorHandlerby absorbing it into the new_ClientCheckoutclass. Reduces per-checkout overhead by ~3 generator frames (~400–500 ns each).Test Plan
just typingpasses (mypy + pyright)just lint-manualpasses (all pre-commit hooks)Checklist
Checklist for Author
Checklist for Reviewer