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

Consider caching the httpx SSL context #279

Closed
MicahLyle opened this issue Jan 16, 2024 · 1 comment
Closed

Consider caching the httpx SSL context #279

MicahLyle opened this issue Jan 16, 2024 · 1 comment
Assignees

Comments

@MicahLyle
Copy link

Describe the bug

When running Django Migrations I noticed that pyuploadcare's FileField makes a network request because an http transport is instantiated. It all runs fine when I run it outside of migrations but when I run it in migrations httpx gets stuck here:

  File "C:\Users\micah\Envs\ad\Lib\site-packages\pyuploadcare\dj\models.py", line 128, in __init__
    super(ImageField, self).__init__(*args, **kwargs)
  File "C:\Users\micah\Envs\ad\Lib\site-packages\pyuploadcare\dj\models.py", line 25, in __init__
    self._client = get_uploadcare_client()
                   ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\micah\Envs\ad\Lib\site-packages\pyuploadcare\dj\client.py", line 18, in get_uploadcare_client
    return Uploadcare(**config)
           ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\micah\Envs\ad\Lib\site-packages\pyuploadcare\client.py", line 130, in __init__
    self.upload_client = Client(
                         ^^^^^^^
  File "C:\Users\micah\Envs\ad\Lib\site-packages\pyuploadcare\api\client.py", line 46, in __init__
    super().__init__(*args, **kwargs)
  File "C:\Users\micah\Envs\ad\Lib\site-packages\httpx\_client.py", line 679, in __init__
    self._transport = self._init_transport(
                      ^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\micah\Envs\ad\Lib\site-packages\httpx\_client.py", line 727, in _init_transport
    return HTTPTransport(
           ^^^^^^^^^^^^^^
  File "C:\Users\micah\Envs\ad\Lib\site-packages\httpx\_transports\default.py", line 126, in __init__
    ssl_context = create_ssl_context(verify=verify, cert=cert, trust_env=trust_env)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\micah\Envs\ad\Lib\site-packages\httpx\_config.py", line 51, in create_ssl_context
    return SSLConfig(
           ^^^^^^^^^^
  File "C:\Users\micah\Envs\ad\Lib\site-packages\httpx\_config.py", line 75, in __init__
    self.ssl_context = self.load_ssl_context()
                       ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\micah\Envs\ad\Lib\site-packages\httpx\_config.py", line 87, in load_ssl_context
    return self.load_ssl_context_verify()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\micah\Envs\ad\Lib\site-packages\httpx\_config.py", line 145, in load_ssl_context_verify
    context.load_verify_locations(cafile=str(ca_bundle_path))

This might be due to a large Insert number of migrations, which can definitely be fixed on my end. however these other issues seem to indicate that caching the SSL context can speed up things dramatically especially if you're instantiating a variable number of clients. On the particular project this is happening on, I have a lot of File fields that are being used and referenced in a lot of migrations so Things seem to be slowed down a lot.

encode/httpx#838 (comment)

encode/httpx#838 (comment)

@evgkirov
Copy link
Collaborator

evgkirov commented Feb 2, 2024

Hi!

Thank you for the suggestion. It significantly improved initialization times.

This fix will be included in the 5.0.1 release.

@evgkirov evgkirov closed this as completed Feb 2, 2024
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

No branches or pull requests

2 participants