diff --git a/NEXT_CHANGELOG.md b/NEXT_CHANGELOG.md index 2ebce050..712ce5cb 100644 --- a/NEXT_CHANGELOG.md +++ b/NEXT_CHANGELOG.md @@ -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 diff --git a/include/databricks_kernel.h b/include/databricks_kernel.h index 0eccb5b1..3a49c1ff 100644 --- a/include/databricks_kernel.h +++ b/include/databricks_kernel.h @@ -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 diff --git a/pyo3/NEXT_CHANGELOG.md b/pyo3/NEXT_CHANGELOG.md index e0881e54..fafda18b 100644 --- a/pyo3/NEXT_CHANGELOG.md +++ b/pyo3/NEXT_CHANGELOG.md @@ -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 diff --git a/pyo3/src/session.rs b/pyo3/src/session.rs index a653fafc..01660521 100644 --- a/pyo3/src/session.rs +++ b/pyo3/src/session.rs @@ -143,6 +143,7 @@ fn build_telemetry_config( #[allow(clippy::too_many_arguments)] fn build_http_config( request_timeout_secs: Option, + max_connections: Option, retry_min_wait_secs: Option, retry_max_wait_secs: Option, retry_max_attempts: Option, @@ -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() @@ -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); } @@ -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 @@ -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, @@ -406,6 +420,7 @@ impl Session { tls_client_cert: Option>, tls_client_key: Option>, request_timeout_secs: Option, + max_connections: Option, retry_min_wait_secs: Option, retry_max_wait_secs: Option, retry_max_attempts: Option, @@ -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, @@ -800,14 +816,14 @@ 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)); @@ -815,14 +831,29 @@ mod send_sync_tests { 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] diff --git a/src/c_abi/session.rs b/src/c_abi/session.rs index 859e22e5..86317dbf 100644 --- a/src/c_abi/session.rs +++ b/src/c_abi/session.rs @@ -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, + /// Maximum idle connections retained per host. `None` keeps the kernel + /// default; populated by `..._set_max_connections`. + max_connections: Option, /// 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` @@ -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, @@ -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 @@ -1385,8 +1409,8 @@ fn parse_scopes(s: &str) -> Option> { } /// 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 @@ -1394,6 +1418,7 @@ fn parse_scopes(s: &str) -> Option> { fn merge_http_config( custom_headers: Vec<(String, String)>, request_timeout: Option, + max_connections: Option, retry: Option, ) -> HttpConfig { let mut http = HttpConfig { @@ -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; @@ -1527,6 +1555,7 @@ fn build_session(config: KernelSessionConfig) -> Result Result