Skip to content

Commit d801265

Browse files
authored
Added connection_pool_size configuration property (preview) (#276)
We have three things that are configurable from `requests` connection pooling perspective: * `pool_connections` - Number of urllib3 connection pools to cache before discarding the least recently used pool. Python requests default value is 10. This PR increases it to 20. * `pool_maxsize` - The maximum number of connections to save in the pool. Improves performance in multithreaded situations. For now, we're setting it to the same value as connection_pool_size. * `pool_block` - If pool_block is False, then more connections will are created, but not saved after the first use. Block when no free connections are available. urllib3 ensures that no more than pool_maxsize connections are used at a time. Prevents platform from flooding. By default, requests library doesn't block. We start with blocking. This PR introduces two new configuration properties: - `connection_pool_size` configuration property, which sets `pool_connections`. Defaults to 20. - `connection_pool_max_size`, which sets `pool_maxsize`. Defaults to the same value as `connection_pool_size`.
1 parent b82fa01 commit d801265

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

databricks/sdk/core.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ class Config:
491491
metadata_service_url = ConfigAttribute(env='DATABRICKS_METADATA_SERVICE_URL',
492492
auth='metadata-service',
493493
sensitive=True)
494+
max_connection_pools: int = ConfigAttribute()
495+
max_connections_per_pool: int = ConfigAttribute()
494496

495497
def __init__(self,
496498
*,
@@ -893,7 +895,31 @@ def __init__(self, cfg: Config = None):
893895

894896
self._session = requests.Session()
895897
self._session.auth = self._authenticate
896-
self._session.mount("https://", HTTPAdapter(max_retries=retry_strategy))
898+
899+
# Number of urllib3 connection pools to cache before discarding the least
900+
# recently used pool. Python requests default value is 10.
901+
pool_connections = cfg.max_connection_pools
902+
if pool_connections is None:
903+
pool_connections = 20
904+
905+
# The maximum number of connections to save in the pool. Improves performance
906+
# in multithreaded situations. For now, we're setting it to the same value
907+
# as connection_pool_size.
908+
pool_maxsize = cfg.max_connections_per_pool
909+
if cfg.max_connections_per_pool is None:
910+
pool_maxsize = pool_connections
911+
912+
# If pool_block is False, then more connections will are created,
913+
# but not saved after the first use. Blocks when no free connections are available.
914+
# urllib3 ensures that no more than pool_maxsize connections are used at a time.
915+
# Prevents platform from flooding. By default, requests library doesn't block.
916+
pool_block = True
917+
918+
http_adapter = HTTPAdapter(max_retries=retry_strategy,
919+
pool_connections=pool_connections,
920+
pool_maxsize=pool_maxsize,
921+
pool_block=pool_block)
922+
self._session.mount("https://", http_adapter)
897923

898924
@property
899925
def account_id(self) -> str:

0 commit comments

Comments
 (0)