Skip to content

Commit 5790335

Browse files
wadellsrhamzeh
authored andcommitted
fix: reuse ssl context in the sync client
This brings ssl context handling in line with the async client. Importantly, openssl has a pretty signifigant performance regression in creating ssl contexts v3.0+ that is mitigated by paying the context creation tax once, instead of for every request. Based on testing, this reduces the openssl v3 performance penalty from ~200ms per connection to 9ms per connection. Original PR: openfga/sdk-generator#607
1 parent 89a39d1 commit 5790335

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

openfga_sdk/sync/rest.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,18 @@ def __init__(
155155
:param pools_size: The number of connection pools to use.
156156
:param maxsize: The maximum number of connections per pool.
157157
"""
158-
if hasattr(configuration, "verify_ssl") and configuration.verify_ssl:
159-
cert_reqs = ssl.CERT_REQUIRED
160-
else:
161-
cert_reqs = ssl.CERT_NONE
158+
# Reuse SSL context to mitigate OpenSSL 3.0+ performance issues
159+
# See: https://github.com/openssl/openssl/issues/17064
160+
ssl_context = ssl.create_default_context(cafile=configuration.ssl_ca_cert)
161+
162+
if configuration.cert_file:
163+
ssl_context.load_cert_chain(
164+
configuration.cert_file, keyfile=configuration.key_file
165+
)
166+
167+
if not configuration.verify_ssl:
168+
ssl_context.check_hostname = False
169+
ssl_context.verify_mode = ssl.CERT_NONE
162170

163171
addition_pool_args = {}
164172

@@ -193,10 +201,7 @@ def __init__(
193201
urllib3.ProxyManager(
194202
num_pools=pools_size,
195203
maxsize=maxsize,
196-
cert_reqs=cert_reqs,
197-
ca_certs=configuration.ssl_ca_cert,
198-
cert_file=configuration.cert_file,
199-
key_file=configuration.key_file,
204+
ssl_context=ssl_context,
200205
proxy_url=configuration.proxy,
201206
proxy_headers=configuration.proxy_headers,
202207
**addition_pool_args,
@@ -208,10 +213,7 @@ def __init__(
208213
self.pool_manager = urllib3.PoolManager(
209214
num_pools=pools_size,
210215
maxsize=maxsize,
211-
cert_reqs=cert_reqs,
212-
ca_certs=configuration.ssl_ca_cert,
213-
cert_file=configuration.cert_file,
214-
key_file=configuration.key_file,
216+
ssl_context=ssl_context,
215217
**addition_pool_args,
216218
)
217219

0 commit comments

Comments
 (0)