Skip to content

Commit

Permalink
Added connection_pool_size configuration property (preview) (#276)
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
nfx authored Aug 10, 2023
1 parent b82fa01 commit d801265
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion databricks/sdk/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,8 @@ class Config:
metadata_service_url = ConfigAttribute(env='DATABRICKS_METADATA_SERVICE_URL',
auth='metadata-service',
sensitive=True)
max_connection_pools: int = ConfigAttribute()
max_connections_per_pool: int = ConfigAttribute()

def __init__(self,
*,
Expand Down Expand Up @@ -893,7 +895,31 @@ def __init__(self, cfg: Config = None):

self._session = requests.Session()
self._session.auth = self._authenticate
self._session.mount("https://", HTTPAdapter(max_retries=retry_strategy))

# Number of urllib3 connection pools to cache before discarding the least
# recently used pool. Python requests default value is 10.
pool_connections = cfg.max_connection_pools
if pool_connections is None:
pool_connections = 20

# 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_maxsize = cfg.max_connections_per_pool
if cfg.max_connections_per_pool is None:
pool_maxsize = pool_connections

# If pool_block is False, then more connections will are created,
# but not saved after the first use. Blocks 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.
pool_block = True

http_adapter = HTTPAdapter(max_retries=retry_strategy,
pool_connections=pool_connections,
pool_maxsize=pool_maxsize,
pool_block=pool_block)
self._session.mount("https://", http_adapter)

@property
def account_id(self) -> str:
Expand Down

0 comments on commit d801265

Please sign in to comment.