Skip to content

Comments

Allow AIOHTTPClient to accept user-provided session or connector (#1736)#1737

Merged
skukutla-stripe merged 3 commits intomasterfrom
skukutla/aiohttp-custom-session-connector
Feb 24, 2026
Merged

Allow AIOHTTPClient to accept user-provided session or connector (#1736)#1737
skukutla-stripe merged 3 commits intomasterfrom
skukutla/aiohttp-custom-session-connector

Conversation

@skukutla-stripe
Copy link
Contributor

Why?

Currently, AIOHTTPClient always creates its own aiohttp.ClientSession and TCPConnector internally, with no way for users to customize them. This means users cannot:

  • Share a connection pool across multiple clients (e.g., Stripe + other services)
  • Tune connection pool settings like limit, limit_per_host, or keepalive_timeout
  • Provide a custom TCPConnector with their own SSL context or DNS caching configuration

The only workaround is setting the private _cached_session attribute after construction, which is an unstable interface.

RequestsClient already accepts an optional session parameter in its constructor (line 585), so there is existing precedent for this pattern. The need is stronger for AIOHTTPClient because async usage inherently involves concurrent requests, making connection pool sharing and tuning a common requirement for people using this client.

What?

  • Added optional session and connector parameters to AIOHTTPClient.init
  • When session is provided, it is used directly as the underlying client session
  • When connector is provided, AIOHTTPClient creates a ClientSession using the user's connector (the SDK still controls the session itself)
  • When neither is provided, behavior is identical to before (creates its own connector with Stripe's CA bundle and a new session)
  • Added _owns_session tracking so that close_async() only closes the session if AIOHTTPClient created it. This avoids destroying a shared session that the user manages themselves (an improvement over RequestsClient.close(), which
    unconditionally closes even user-provided sessions)
  • This is a backward-compatible change: both new parameters default to None, preserving existing behavior for all current users

See Also

@skukutla-stripe skukutla-stripe self-assigned this Feb 24, 2026
@skukutla-stripe skukutla-stripe requested a review from a team as a code owner February 24, 2026 19:29
@ksealmalaysia41-lang

This comment was marked as spam.

Copy link
Member

@xavdid-stripe xavdid-stripe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! had a couple of small style suggestions. once you fix those, you can merge!

@skukutla-stripe skukutla-stripe enabled auto-merge (squash) February 24, 2026 21:30
@skukutla-stripe skukutla-stripe merged commit a643eac into master Feb 24, 2026
21 checks passed
@skukutla-stripe skukutla-stripe deleted the skukutla/aiohttp-custom-session-connector branch February 24, 2026 21:31
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.

3 participants