-
-
Notifications
You must be signed in to change notification settings - Fork 883
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
SSLContext get created unconditionally which can cause serious performance issues #2298
Comments
I think you'd find the same behaviour here with I'd suggest either reusing the same transport as @florimondmanca suggested in #2245. You could also just share a single ssl context by creating a global SSL context instance that you pass in to each client with For now I'd suggest we close this off, as this isn't an issue when you:
I do think there's some possible scope for improvement here, both around the API for SSL configuration, and potentially also for reusing rather than recreating SSL contexts, but at this point in time I don't think we'd want to consider either of those as being in scope. |
Avoid to create a SSLContext in AsyncHTTPTransportNoHttp See: * https://github.com/encode/httpx/blob/0f61aa58d66680c239ce43c8cdd453e7dc532bfc/httpx/_transports/default.py#L271 * encode/httpx#2298
Avoid to create a SSLContext in AsyncHTTPTransportNoHttp See: * https://github.com/encode/httpx/blob/0f61aa58d66680c239ce43c8cdd453e7dc532bfc/httpx/_transports/default.py#L271 * encode/httpx#2298
Discussed in #2245
Originally posted by kissgyorgy May 25, 2022
Our use case is that we built an API Gateway, in which we use httpx as a client to connect to the backend service.
We have a multi-tenant system, so we make a new httpx client every time to isolate customer requests completely.
We have a serious performance issue this way, we measured it, and every request takes 100ms longer just because a
ssl.SSLContext
is created unconditionally, so we spend a lot of time buildingSSLContext
s, which we don't need anyway, because we don't need an HTTPS connection to the backend, as the two services are on the same machines.I tried to solve it by passing in a global
httpx.AsyncHTTPTransport
, but httpx closes the transport after finishing the request, and I was very confused by that so I didn't thought it could be solved this way.The next thing I tried to implement a global connection pool, so the
SSLContext
would be created at application startup only once, but because of a serious bug inhttpcore
about corrupted connections, we couldn't do that either (we would have hit the same bug with the global transport implementation too).A trivial implementation would be to only create
ssl.SSLContext
in case of HTTPS connections, which httpcore is already doing correctly, but for some reason, httpx does this unconditionally.Another implementation idea by @vlaci that there could be an
ssl_context_factory
, which could be implemented by users.Here is a py-spy snapshot analyzed with speedscope, which shows we spend half the time creating unused

SSLContext
s:The text was updated successfully, but these errors were encountered: