8000 Beta: disable HTTPX connection pooling on the default client by richardm-stripe · Pull Request #1248 · stripe/stripe-python · GitHub
[go: up one dir, main page]

Skip to content

Beta: disable HTTPX connection pooling on the default client #1248

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

Conversation

richardm-stripe
Copy link
Contributor

A user reported some friction using async methods with unittest which tries to run each test in an isolated event loop.

This is because HTTPX attempts to re-use connections by default, and re-using a connection across two different event loops triggers an error.

We can avoid the error by disabling connection pooling. This causes requests to succeed in more circumstances, at the expense of making requests more wasteful and hurting performance. Users who wish to avoid this should initialize their own http client, e.g. stripe.default_http_client = stripe.HTTPXClient()

@pakrym-stripe
Copy link
Contributor

Are we sacrificing real-world performance to fix unit tests?

@richardm-stripe
Copy link
Contributor Author

In a sense? I don't consider our own test cases to be a part of the "real world", but I do think that users writing tests is an important real world use case for us. The tradeoff here is to reduce friction for (some) users writing tests, at the expense of requiring more work from users who want to minimize resource usage (and changing the default to be more wasteful for users who haven't thought about either).

HTTPX makes a similar design decision itself, i.e. if you use the globals and go httpx.request(...) it won't use connection pooling, it only uses connection pooling when you client = httpx.Client(); client.request(...).

@richardm-stripe
Copy link
Contributor Author

Maybe we can do something similar to how we handle connection pooling for Requests though, where we make a thread-local connection pool.

self._thread_local.session = (
self._session or self.requests.Session()
)

Maybe there is some concept of a "event-loop-local" variable.

@richardm-stripe
Copy link
Contributor Author
richardm-stripe commented Feb 23, 2024

Oh interesting, I just read encode/httpx#2058 (comment) where @Zac-HD is recommending that httpx do that very thing, but upstream.

@richardm-stripe
Copy link
Contributor Author

I'm going to go ahead and close unmerged, I think it's premature & so far it looks like the user having the event loop troubles is happy to work around it. There's no real reason we couldn't address this later.

@xavdid-stripe xavdid-stripe deleted the richardm-disable-httpx-connection-pooling-on-default-client branch May 10, 2024 03:37
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
0