Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ changes go in their respective binding's NEXT_CHANGELOG.

### Added

- Add a C ABI setter for the maximum idle HTTP connections retained per host.

### Changed

### Updated
Expand Down
6 changes: 6 additions & 0 deletions include/databricks_kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,12 @@ KernelStatusCode kernel_session_config_set_custom_header(KernelSessionConfig* co
KernelStatusCode kernel_session_config_set_request_timeout(KernelSessionConfig* config,
uint64_t request_timeout_ms);

/* Configure the maximum number of idle HTTP connections retained per host.
* `max_connections` must be greater than zero. Omitting this setter keeps the
* kernel default (100). */
KernelStatusCode kernel_session_config_set_max_connections(KernelSessionConfig* config,
size_t max_connections);

/* Initialize kernel logging, process-wide and ONCE (first non-OFF call wins;
* later calls are no-ops). `level` is OFF/ERROR/WARN/INFO/DEBUG/TRACE
* (NULL → RUST_LOG, default warn); `file_path` NULL → stderr. Not tied to
Expand Down
2 changes: 2 additions & 0 deletions pyo3/NEXT_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ changes go in the root `NEXT_CHANGELOG.md`.

### Added

- Add `Session(..., max_connections=...)` to configure idle HTTP connections per host.

### Changed

### Updated
Expand Down
45 changes: 38 additions & 7 deletions pyo3/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ fn build_telemetry_config(
#[allow(clippy::too_many_arguments)]
fn build_http_config(
request_timeout_secs: Option<f64>,
max_connections: Option<usize>,
retry_min_wait_secs: Option<u64>,
retry_max_wait_secs: Option<u64>,
retry_max_attempts: Option<u32>,
Expand Down Expand Up @@ -172,8 +173,14 @@ fn build_http_config(
}
}
};
if max_connections == Some(0) {
return Err(pyo3::exceptions::PyValueError::new_err(
"max_connections must be greater than 0",
));
}
let has_http_headers = http_headers.as_ref().is_some_and(|h| !h.is_empty());
if request_timeout.is_none()
&& max_connections.is_none()
&& retry_min_wait_secs.is_none()
&& retry_max_wait_secs.is_none()
&& retry_max_attempts.is_none()
Expand All @@ -187,6 +194,9 @@ fn build_http_config(
if let Some(timeout) = request_timeout {
http.request_timeout = timeout;
}
if let Some(max) = max_connections {
http.pool_max_idle_per_host = max;
}
if let Some(s) = retry_min_wait_secs {
http.retry_min_wait = std::time::Duration::from_secs(s);
}
Expand Down Expand Up @@ -299,6 +309,9 @@ impl Session {
/// is bounded by the shorter limit. Unset or `0` keeps the kernel default
/// (120 seconds).
///
/// `max_connections` sets the maximum idle connections retained per host.
/// Unset keeps the kernel default (100); zero is rejected.
///
/// `http_headers` is a list of `(name, value)` pairs sent on every
/// request — used to forward the connector's caller headers (which
/// also carry the composed `User-Agent`). The kernel applies them
Expand Down Expand Up @@ -342,6 +355,7 @@ impl Session {
tls_client_cert=None,
tls_client_key=None,
request_timeout_secs=None,
max_connections=None,
retry_min_wait_secs=None,
retry_max_wait_secs=None,
retry_max_attempts=None,
Expand Down Expand Up @@ -406,6 +420,7 @@ impl Session {
tls_client_cert: Option<Vec<u8>>,
tls_client_key: Option<Vec<u8>>,
request_timeout_secs: Option<f64>,
max_connections: Option<usize>,
retry_min_wait_secs: Option<u64>,
retry_max_wait_secs: Option<u64>,
retry_max_attempts: Option<u32>,
Expand Down Expand Up @@ -522,6 +537,7 @@ impl Session {
// `Authorization` / `x-databricks-org-id`, which always win.
let http_config = build_http_config(
request_timeout_secs,
max_connections,
retry_min_wait_secs,
retry_max_wait_secs,
retry_max_attempts,
Expand Down Expand Up @@ -800,29 +816,44 @@ mod send_sync_tests {
#[test]
fn request_timeout_kwarg_maps_to_http_config() {
let default = databricks_sql_kernel::HttpConfig::default();
assert!(build_http_config(None, None, None, None, None, None)
assert!(build_http_config(None, None, None, None, None, None, None)
.unwrap()
.is_none());
assert!(build_http_config(Some(0.0), None, None, None, None, None)
assert!(build_http_config(Some(0.0), None, None, None, None, None, None)
.unwrap()
.is_none());

let http = build_http_config(Some(0.5), None, None, None, None, None)
let http = build_http_config(Some(0.5), None, None, None, None, None, None)
.unwrap()
.expect("positive request timeout should build an HTTP config");
assert_eq!(http.request_timeout, std::time::Duration::from_millis(500));
assert_eq!(http.retry_min_wait, default.retry_min_wait);
assert_eq!(http.overall_timeout, default.overall_timeout);

let one_ns = std::time::Duration::from_nanos(1).as_secs_f64();
assert!(build_http_config(Some(one_ns / 1_000.0), None, None, None, None, None).is_err());
let http = build_http_config(Some(one_ns), None, None, None, None, None)
assert!(
build_http_config(Some(one_ns / 1_000.0), None, None, None, None, None, None)
.is_err()
);
let http = build_http_config(Some(one_ns), None, None, None, None, None, None)
.unwrap()
.expect("one-nanosecond request timeout should be accepted");
assert_eq!(http.request_timeout, std::time::Duration::from_nanos(1));

assert!(build_http_config(Some(-1.0), None, None, None, None, None).is_err());
assert!(build_http_config(Some(f64::NAN), None, None, None, None, None).is_err());
assert!(build_http_config(Some(-1.0), None, None, None, None, None, None).is_err());
assert!(build_http_config(Some(f64::NAN), None, None, None, None, None, None).is_err());
}

#[test]
fn max_connections_kwarg_maps_to_http_config() {
let default = databricks_sql_kernel::HttpConfig::default();
let http = build_http_config(None, Some(17), None, None, None, None, None)
.unwrap()
.expect("max connections should build an HTTP config");
assert_eq!(http.pool_max_idle_per_host, 17);
assert_eq!(http.request_timeout, default.request_timeout);

assert!(build_http_config(None, Some(0), None, None, None, None, None).is_err());
}

#[test]
Expand Down
93 changes: 86 additions & 7 deletions src/c_abi/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ pub struct KernelSessionConfig {
/// Total request timeout (populated by `..._set_request_timeout`). `None`
/// keeps [`HttpConfig::default`]'s 120-second timeout.
request_timeout: Option<Duration>,
/// Maximum idle connections retained per host. `None` keeps the kernel
/// default; populated by `..._set_max_connections`.
max_connections: Option<usize>,
/// HTTP retry / backoff knobs (populated by `..._set_retry_config`).
/// `None` keeps the kernel default policy (exponential backoff with
/// jitter; see [`HttpConfig::default`] for the concrete values); `Some`
Expand Down Expand Up @@ -263,6 +266,7 @@ pub unsafe extern "C" fn kernel_session_config_new(
tls: TlsSpec::default(),
custom_headers: Vec::new(),
request_timeout: None,
max_connections: None,
retry: None,
// Default to ignoring transactions, matching IgnoreTransactions=1.
ignore_transactions: true,
Expand Down Expand Up @@ -603,6 +607,26 @@ pub unsafe extern "C" fn kernel_session_config_set_request_timeout(
})
}

/// Configure the maximum number of idle HTTP connections retained per host.
/// The value must be greater than zero; omitting this setter keeps the kernel
/// default (100).
///
/// # Safety
/// `config` must be a live config handle.
#[no_mangle]
pub unsafe extern "C" fn kernel_session_config_set_max_connections(
config: *mut KernelSessionConfig,
max_connections: usize,
) -> KernelStatusCode {
set_on_config(config, |c| {
if max_connections == 0 {
return Err(Error::invalid_argument().message("max_connections must be greater than 0"));
}
c.max_connections = Some(max_connections);
Ok(())
})
}

/// Configure the HTTP retry / backoff policy: the minimum and maximum
/// backoff wait between attempts (milliseconds), the maximum number of
/// retries after the initial attempt (so a request is tried at most
Expand Down Expand Up @@ -1385,15 +1409,16 @@ fn parse_scopes(s: &str) -> Option<Vec<String>> {
}

/// Fold the C-side HTTP overrides onto a fresh [`HttpConfig`]. We start from
/// defaults and set only the fields a setter populated, preserving pool size
/// and any untouched retry or timeout knobs.
/// defaults and set only the fields a setter populated, preserving untouched
/// pool, retry, and timeout knobs.
///
/// This is the code that actually configures live HTTP behavior (called from
/// `build_session` whenever an HTTP override was set), so it is a pure helper
/// kept unit-testable without a live session.
fn merge_http_config(
custom_headers: Vec<(String, String)>,
request_timeout: Option<Duration>,
max_connections: Option<usize>,
retry: Option<RetrySpec>,
) -> HttpConfig {
let mut http = HttpConfig {
Expand All @@ -1403,6 +1428,9 @@ fn merge_http_config(
if let Some(t) = request_timeout {
http.request_timeout = t;
}
if let Some(max) = max_connections {
http.pool_max_idle_per_host = max;
}
if let Some(r) = retry {
http.retry_min_wait = r.min_wait;
http.retry_max_wait = r.max_wait;
Expand Down Expand Up @@ -1527,6 +1555,7 @@ fn build_session(config: KernelSessionConfig) -> Result<Session, KernelStatusCod
tls,
custom_headers,
request_timeout,
max_connections,
retry,
ignore_transactions,
identity_federation_client_id,
Expand Down Expand Up @@ -1586,8 +1615,17 @@ fn build_session(config: KernelSessionConfig) -> Result<Session, KernelStatusCod

// Skip `.http()` when no HTTP override was set, so the kernel's
// full `HttpConfig::default()` applies untouched.
if !custom_headers.is_empty() || request_timeout.is_some() || retry.is_some() {
builder = builder.http(merge_http_config(custom_headers, request_timeout, retry));
if !custom_headers.is_empty()
|| request_timeout.is_some()
|| max_connections.is_some()
|| retry.is_some()
{
builder = builder.http(merge_http_config(
custom_headers,
request_timeout,
max_connections,
retry,
));
}

// Programmatic proxy (env-var behaviour preserved when unset).
Expand Down Expand Up @@ -2468,6 +2506,31 @@ mod tests {
}
}

#[test]
fn set_max_connections_records_positive_value_and_rejects_zero() {
unsafe {
let mut config: *mut KernelSessionConfig = std::ptr::null_mut();
assert_eq!(
kernel_session_config_new(&mut config),
KernelStatusCode::Success
);
assert!(config.as_ref().unwrap().max_connections.is_none());

assert_eq!(
kernel_session_config_set_max_connections(config, 37),
KernelStatusCode::Success
);
assert_eq!(config.as_ref().unwrap().max_connections, Some(37));

assert_eq!(
kernel_session_config_set_max_connections(config, 0),
KernelStatusCode::InvalidArgument
);
assert_eq!(config.as_ref().unwrap().max_connections, Some(37));
kernel_session_config_free(config);
}
}

/// The retry setter rejects a zero min_wait with `InvalidArgument` (a zero
/// floor is genuinely invalid), but a `max_wait < min_wait` pair is a tuning
/// typo that is CORRECTED (max raised to min) with a warning rather than
Expand Down Expand Up @@ -3024,6 +3087,7 @@ mod tests {
let http = merge_http_config(
Vec::new(),
None,
None,
Some(RetrySpec {
min_wait: Duration::from_millis(500),
max_wait: Duration::from_millis(30_000),
Expand All @@ -3046,6 +3110,7 @@ mod tests {
let http = merge_http_config(
Vec::new(),
None,
None,
Some(RetrySpec {
min_wait: Duration::from_millis(1),
max_wait: Duration::from_millis(1_000),
Expand All @@ -3070,6 +3135,7 @@ mod tests {
let retry_only = merge_http_config(
Vec::new(),
None,
None,
Some(RetrySpec {
min_wait: Duration::from_millis(250),
max_wait: Duration::from_millis(5_000),
Expand All @@ -3083,8 +3149,12 @@ mod tests {

// Headers-only: header carried through, retry knobs untouched from default.
let default = HttpConfig::default();
let headers_only =
merge_http_config(vec![("X-Test".to_string(), "1".to_string())], None, None);
let headers_only = merge_http_config(
vec![("X-Test".to_string(), "1".to_string())],
None,
None,
None,
);
assert_eq!(
headers_only.custom_headers,
vec![("X-Test".to_string(), "1".to_string())]
Expand All @@ -3098,9 +3168,18 @@ mod tests {
#[test]
fn merge_http_config_maps_request_timeout_and_preserves_other_defaults() {
let default = HttpConfig::default();
let http = merge_http_config(Vec::new(), Some(Duration::from_millis(4_500)), None);
let http = merge_http_config(Vec::new(), Some(Duration::from_millis(4_500)), None, None);
assert_eq!(http.request_timeout, Duration::from_millis(4_500));
assert_eq!(http.retry_min_wait, default.retry_min_wait);
assert_eq!(http.overall_timeout, default.overall_timeout);
}

#[test]
fn merge_http_config_maps_max_connections_and_preserves_other_defaults() {
let default = HttpConfig::default();
let http = merge_http_config(Vec::new(), None, Some(23), None);
assert_eq!(http.pool_max_idle_per_host, 23);
assert_eq!(http.request_timeout, default.request_timeout);
assert_eq!(http.retry_max_retries, default.retry_max_retries);
}
}
5 changes: 5 additions & 0 deletions tests/c/session_smoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,11 @@ static void test_proxy_and_tls_setters(void) {
rc = kernel_session_config_set_request_timeout(config, 0);
CHECK(rc == KernelStatusCode_Success, "set_request_timeout (default) succeeds");

rc = kernel_session_config_set_max_connections(config, 64);
CHECK(rc == KernelStatusCode_Success, "set_max_connections succeeds");
rc = kernel_session_config_set_max_connections(config, 0);
CHECK(rc == KernelStatusCode_InvalidArgument, "set_max_connections (zero) is rejected");

/* Retry config: a valid range (incl. max_retries=0 to disable, overall
* timeout 0 = keep default) succeeds; a non-zero overall timeout also
* succeeds; a zero min_wait is rejected; an inverted range (max<min) is a
Expand Down
Loading