Skip to content

perf(core): add lazy imports, __slots__, TLS context caching, and lazy sessions - #175

Draft
skupriienko wants to merge 1 commit into
wave-2-transport-redactionfrom
wave-3-boot-slots-pooling
Draft

skupriienko wants to merge 1 commit into
wave-2-transport-redactionfrom
wave-3-boot-slots-pooling

Conversation

@skupriienko

@skupriienko skupriienko commented Nov 15, 2026 •

Copy link
Copy Markdown
Collaborator

Stack (v1.9.2 Hardening Rollout)

Wave PR Base <- Head Scope
Wave 1 #173 master <- wave-1-tooling-hygiene CRIT-03, CRIT-04, CRIT-05, HIGH-06
Wave 2 #174 wave-1-tooling-hygiene <- wave-2-transport-redaction CRIT-01, CRIT-02, HIGH-01 (lock), HIGH-02, HIGH-03, HIGH-04 (path), OPT-09
-> Wave 3 (This PR) #175 wave-2-transport-redaction <- wave-3-boot-slots-pooling OPT-01..OPT-07, OPT-10, OPT-11, HIGH-01 (warn_unclosed)
Wave 4 #176 wave-3-boot-slots-pooling <- wave-4-builders-semver HIGH-04 (bool/stream), HIGH-05, HIGH-07, OPT-08
Wave 5 #177 wave-4-builders-semver <- wave-5-typing-stateful-tests MOD-02, MOD-04

Merge in order: #173 -> #174 -> #175 -> #176 -> #177.

Note

Stacked PR workflow note: ClusterFuzzLite PR fuzzing runs immediately on all PRs. Workflows scoped to branches: [master] (CI, PR Validation, Continuous Security Verification, CodeQL) trigger automatically as parent PRs merge into master and GitHub retargets this PR's base branch to master.


Summary

Wave 3 of 5 for v1.9.2 (stacked on #174 / wave-2-transport-redaction). Reduces cold-boot import overhead via PEP 562 lazy top-level exports and isolated utils/transport_security.py, applies __slots__ across Client, Config, Endpoint, and builders (slotscheck-clean across 30 classes), makes requests.Session lazy on Client, caches TLSv1_2+ SSLContext instances keyed by (ca_bundle, st_mtime_ns, st_size), and adds opt-in Config.warn_unclosed=False lifecycle warnings.


Actions

  • Lazy Top-Level Facade & Transport Isolation (mailjet_rest/__init__.py, mailjet_rest/utils/transport_security.py, mailjet_rest/utils/guardrails.py, OPT-01..OPT-05, OPT-10):
    • Replaced eager top-level imports in mailjet_rest/__init__.py with PEP 562 __getattr__ lazy resolution and TYPE_CHECKING stubs.
    • Extracted SecretAuth, SecureHTTPAdapter, and JitterRetry into mailjet_rest/utils/transport_security.py and cached TLSv1_2+ SSLContext instances keyed by (ca_bundle, st_mtime_ns, st_size) so CA file updates invalidate cleanly.
  • __slots__, Lazy Session Allocation & Opt-In Close Warnings (mailjet_rest/client.py, mailjet_rest/config.py, mailjet_rest/endpoint.py, mailjet_rest/builders.py, OPT-06, OPT-07, OPT-11, HIGH-01):
    • Added __slots__ across SDK configuration, endpoint, client, and builder classes (keeping SecretAuth unslotted per requests.auth.AuthBase for slotscheck compliance).
    • Deferred requests.Session creation until the first request and added Config.warn_unclosed: bool = False in Client.__del__ for opt-in ResourceWarning diagnostics without breaking existing 1.x test suites.
  • Boot, Slots & Lifecycle Tests (tests/unit/test_boot_and_slots.py, tests/property/test_client_properties.py):
    • Added unit and property coverage for cold-boot import isolation, slotscheck guarantees, SSLContext cache invalidation on CA changes, lazy session creation, and Config.warn_unclosed.

Verification & Testing

  • Verified offline checks across the v1.9.2 stack: ruff check ., ruff format --check ., mypy --strict mailjet_rest, pyright mailjet_rest, slotscheck -m mailjet_rest (13 modules, 30 classes, 0 errors), python tests/test_boot.py (0.544s cProfile / 0.8364s wall), pytest -q tests/test_perf.py (6 passed), and pytest -n auto -W error tests/unit tests/property tests/regression.
  • Live Mailjet API network tests (pytest -m network, samples/smoke_readme_runner.py) were skipped locally due to absent MJ_APIKEY_PUBLIC / MJ_APIKEY_PRIVATE credentials.

This branch has not been deployed

No deployments
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.

1 participant