Skip to content
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

fix(ssl): add lru_cache for create_ssl_context #3053

Closed
wants to merge 2 commits into from

Conversation

shepilov-vladislav
Copy link

@shepilov-vladislav shepilov-vladislav commented Jan 12, 2024

Summary

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@T-256
Copy link
Contributor

T-256 commented Jan 14, 2024

Thanks for the PR, I like the solution. This is high memory usage I encountered when using tons of Client instance in an app (I need them for several reasons).

But perhaps you would like to see and wait for #3022

@karpetrosyan could new httpx.SSLContext cachable?

@karpetrosyan
Copy link
Member

Are we really capable of doing something like this? What if the SSL context is modified? Why should any subsequent function call return the modified SSL context?

I mean...

import ssl
from functools import cache

@cache
def get_ssl_context():
    return ssl.create_default_context()

first_ssl = get_ssl_context()
first_ssl.check_hostname = False

second_ssl = get_ssl_context()
print(f.check_hostname) # False

@shepilov-vladislav
Copy link
Author

@T-256, @karpetrosyan First of all thank you for your quick response and help.

The thing is that we make a lot of requests using http and we tried to replace httpx with aiohttp and saw that CPU consumption decreased many times, after that I went to compare the code base and saw that aiohttp just caches the creation of SSLContext - see here.

After adding caching - CPU consumption dropped by more than 4x on our Pods. If there is another more correct way to do it - please advise. I haven't found a way to do it without changing the httpx codebase.

Perhaps this PR will provide that opportunity, but I'm not sure

If you still find my changes useful, just tell me, I'll fix the tests and we'll merge this one out ✌️

@karpetrosyan
Copy link
Member

We should probably wait until #3022 is marged before we consider adding cache at that layer. I like this idea, because configuring SSL context is a very expensive job.

Ref: karpetrosyan/hishel#139

@tomchristie
Copy link
Member

After adding caching - CPU consumption dropped by more than 4x on our Pods.

Prob indicates that you're instantiating clients repeatedly, and losing out on the benefits of connection pooling.

We should probably wait until #3022 is marged before we consider adding cache at that layer.

Would be simpler to consider then yep. (Anyone want to open a "SSL Cache" discussion?)

@T-256
Copy link
Contributor

T-256 commented Sep 23, 2024

Anyone want to open a "SSL Cache" discussion?

I don't think we would need that. by refer to this comment, it'd be do the same job of caching to use single global variable cross clients.
Also, for whose using default ssl contexts, As you said in here it's good to have DEFAULT_SSL_CONTEXT = httpx.SSLContext() that used by Client by default.

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.

4 participants