Run SSLConfig cert loading in threadpool (#209)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Can Sarıgöl · committed by Seth Michael Larson · parent bebedcb · Browse files · 3 files·+30 added−38 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+30 added−38 removed
- tests:+18 added−27 removed
- other:+12 added−11 removed
Provenance
- Commit
c5248e6- Tree
419d92b- Author
Can Sarıgöl <cansarigol@derinbilgi.com.tr>- Committer
Seth Michael Larson <sethmichaellarson@gmail.com>- Parent
bebedcb
3 files changed+30 added−38 removed
modified:httpx/dispatch/connection.py+6 added-1 removed
64 unmodified lines65 timeout = self.timeout if timeout is None else TimeoutConfig(timeout)66 67 host = self.origin.host68 port = self.origin.port69- ssl_context = await ssl.load_ssl_context() if self.origin.is_ssl else None69+ # Run the SSL loading in a threadpool, since it makes disk accesses.70+ ssl_context = (71+ await self.backend.run_in_threadpool(ssl.load_ssl_context)72+ if self.origin.is_ssl73+ else None74+ )75 76 if self.release_func is None:77 on_release = None78 else:38 unmodified linesmodified:httpx/config.py+6 added-10 removed
1-import asyncio1 import ssl2 import typing3 from pathlib import Path4 58 unmodified lines63 if (cert == self.cert) and (verify == self.verify):64 return self65 return SSLConfig(cert=cert, verify=verify)66 68- async def load_ssl_context(self) -> ssl.SSLContext:67+ def load_ssl_context(self) -> ssl.SSLContext:68 if self.ssl_context is None:70- if not self.verify:71- self.ssl_context = self.load_ssl_context_no_verify()72- else:73- # Run the SSL loading in a threadpool, since it makes disk accesses.74- loop = asyncio.get_event_loop()75- self.ssl_context = await loop.run_in_executor(76- None, self.load_ssl_context_verify77- )69+ self.ssl_context = (70+ self.load_ssl_context_verify()71+ if self.verify72+ else self.load_ssl_context_no_verify()73+ )74 75 assert self.ssl_context is not None76 return self.ssl_context77 171 unmodified linesmodified:tests/test_config.py+18 added-27 removed
3 unmodified lines4 5 import httpx6 7 8-@pytest.mark.asyncio9-async def test_load_ssl_config():8+def test_load_ssl_config():9 ssl_config = httpx.SSLConfig()11- context = await ssl_config.load_ssl_context()10+ context = ssl_config.load_ssl_context()11 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED12 assert context.check_hostname is True13 14 16-@pytest.mark.asyncio17-async def test_load_ssl_config_verify_non_existing_path():15+def test_load_ssl_config_verify_non_existing_path():16 ssl_config = httpx.SSLConfig(verify="/path/to/nowhere")17 with pytest.raises(IOError):20- await ssl_config.load_ssl_context()18+ ssl_config.load_ssl_context()19 20 23-@pytest.mark.asyncio24-async def test_load_ssl_config_verify_existing_file():21+def test_load_ssl_config_verify_existing_file():22 ssl_config = httpx.SSLConfig(verify=httpx.config.DEFAULT_CA_BUNDLE_PATH)26- context = await ssl_config.load_ssl_context()23+ context = ssl_config.load_ssl_context()24 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED25 assert context.check_hostname is True26 27 31-@pytest.mark.asyncio32-async def test_load_ssl_config_verify_directory():28+def test_load_ssl_config_verify_directory():29 path = httpx.config.DEFAULT_CA_BUNDLE_PATH.parent30 ssl_config = httpx.SSLConfig(verify=path)35- context = await ssl_config.load_ssl_context()31+ context = ssl_config.load_ssl_context()32 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED33 assert context.check_hostname is True34 35 40-@pytest.mark.asyncio41-async def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):36+def test_load_ssl_config_cert_and_key(cert_pem_file, cert_private_key_file):37 ssl_config = httpx.SSLConfig(cert=(cert_pem_file, cert_private_key_file))43- context = await ssl_config.load_ssl_context()38+ context = ssl_config.load_ssl_context()39 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED40 assert context.check_hostname is True41 42 48-@pytest.mark.asyncio43 @pytest.mark.parametrize("password", [b"password", "password"])50-async def test_load_ssl_config_cert_and_encrypted_key(44+def test_load_ssl_config_cert_and_encrypted_key(45 cert_pem_file, cert_encrypted_private_key_file, password46 ):47 ssl_config = httpx.SSLConfig(48 cert=(cert_pem_file, cert_encrypted_private_key_file, password)49 )56- context = await ssl_config.load_ssl_context()50+ context = ssl_config.load_ssl_context()51 assert context.verify_mode == ssl.VerifyMode.CERT_REQUIRED52 assert context.check_hostname is True53 54 61-@pytest.mark.asyncio62-async def test_load_ssl_config_cert_and_key_invalid_password(55+def test_load_ssl_config_cert_and_key_invalid_password(56 cert_pem_file, cert_encrypted_private_key_file57 ):58 ssl_config = httpx.SSLConfig(59 cert=(cert_pem_file, cert_encrypted_private_key_file, "password1")60 )61 62 with pytest.raises(ssl.SSLError):70- await ssl_config.load_ssl_context()63+ ssl_config.load_ssl_context()64 65 73-@pytest.mark.asyncio74-async def test_load_ssl_config_cert_without_key_raises(cert_pem_file):66+def test_load_ssl_config_cert_without_key_raises(cert_pem_file):67 ssl_config = httpx.SSLConfig(cert=cert_pem_file)68 with pytest.raises(ssl.SSLError):77- await ssl_config.load_ssl_context()69+ ssl_config.load_ssl_context()70 71 80-@pytest.mark.asyncio81-async def test_load_ssl_config_no_verify():72+def test_load_ssl_config_no_verify():73 ssl_config = httpx.SSLConfig(verify=False)83- context = await ssl_config.load_ssl_context()74+ context = ssl_config.load_ssl_context()75 assert context.verify_mode == ssl.VerifyMode.CERT_NONE76 assert context.check_hostname is False77 78 46 unmodified lines