Skip to content

Commit 856f95e

Browse files
committed
feat(bindings): expose request timeout to Python and C
Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
1 parent 65ac4c1 commit 856f95e

8 files changed

Lines changed: 197 additions & 44 deletions

File tree

‎NEXT_CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ changes go in their respective binding's NEXT_CHANGELOG.
1212

1313
### Added
1414

15+
- C ABI: `kernel_session_config_set_request_timeout()` configures the timeout
16+
for one HTTP request in milliseconds; `0` keeps the kernel's 120-second
17+
default. This enables Go and other C-ABI consumers to expose request-timeout
18+
configuration.
19+
1520
### Changed
1621

1722
### Updated

‎include/databricks_kernel.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,12 @@ KernelStatusCode kernel_session_config_set_session_conf(KernelSessionConfig* con
336336
KernelStatusCode kernel_session_config_set_custom_header(KernelSessionConfig* config,
337337
const char* name, const char* value);
338338

339+
/* Configure the timeout for one HTTP request, from connect through response
340+
* body completion. `request_timeout_ms == 0` keeps the kernel default (120s);
341+
* a positive value overrides it. */
342+
KernelStatusCode kernel_session_config_set_request_timeout(KernelSessionConfig* config,
343+
uint64_t request_timeout_ms);
344+
339345
/* Initialize kernel logging, process-wide and ONCE (first non-OFF call wins;
340346
* later calls are no-ops). `level` is OFF/ERROR/WARN/INFO/DEBUG/TRACE
341347
* (NULL → RUST_LOG, default warn); `file_path` NULL → stderr. Not tied to

‎pyo3/Cargo.toml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ license = "Apache-2.0"
3939
description = "PyO3 Python bindings for the Databricks SQL Kernel"
4040
repository = "https://github.com/databricks/databricks-sql-kernel"
4141

42+
[features]
43+
# Unit-test binaries must link libpython. Maturin enables this feature only
44+
# when building the extension module, where Python provides the symbols.
45+
extension-module = ["pyo3/extension-module"]
46+
4247
# Built as a Python extension module by maturin. The lib basename here
4348
# only affects the intermediate `.so` cargo produces; maturin renames
4449
# the wheel artifact to `databricks_sql_kernel.<abi>.so` per
@@ -72,7 +77,7 @@ databricks-sql-kernel = { path = "..", default-features = false, features = ["tl
7277
# step with `requires-python = ">=3.9"` in pyproject.toml; bumping
7378
# both together avoids shipping a wheel whose ABI tag advertises an
7479
# interpreter the package no longer claims to support.
75-
pyo3 = { version = "0.29", features = ["extension-module", "abi3-py39"] }
80+
pyo3 = { version = "0.29", features = ["abi3-py39"] }
7681

7782
# Multi-thread runtime — one per Session, owned on the Rust side.
7883
tokio = { version = "1", features = ["rt-multi-thread"] }

‎pyo3/NEXT_CHANGELOG.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ changes go in the root `NEXT_CHANGELOG.md`.
1414

1515
### Added
1616

17+
- `Session(..., request_timeout_secs=...)` configures the per-request HTTP
18+
timeout; unset or `0` keeps the kernel's 120-second default.
19+
1720
### Changed
1821

1922
### Updated

‎pyo3/pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ dev = [
5757

5858
[tool.maturin]
5959
module-name = "databricks_sql_kernel"
60+
features = ["extension-module"]

‎pyo3/src/session.rs‎

Lines changed: 90 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,57 @@ fn build_telemetry_config(
9595
Ok(telemetry)
9696
}
9797

98+
#[allow(clippy::too_many_arguments)]
99+
fn build_http_config(
100+
request_timeout_secs: Option<f64>,
101+
retry_min_wait_secs: Option<u64>,
102+
retry_max_wait_secs: Option<u64>,
103+
retry_max_attempts: Option<u32>,
104+
retry_overall_timeout_secs: Option<u64>,
105+
http_headers: Option<Vec<(String, String)>>,
106+
) -> PyResult<Option<databricks_sql_kernel::HttpConfig>> {
107+
// Zero means "keep the kernel default", matching the C ABI setter. Use a
108+
// float because databricks-sql-python's existing `_socket_timeout` accepts
109+
// fractional seconds.
110+
let request_timeout = match request_timeout_secs {
111+
None | Some(0.0) => None,
112+
Some(s) => Some(std::time::Duration::try_from_secs_f64(s).map_err(|_| {
113+
pyo3::exceptions::PyValueError::new_err("request_timeout_secs must be finite and >= 0")
114+
})?),
115+
};
116+
let has_http_headers = http_headers.as_ref().is_some_and(|h| !h.is_empty());
117+
if request_timeout.is_none()
118+
&& retry_min_wait_secs.is_none()
119+
&& retry_max_wait_secs.is_none()
120+
&& retry_max_attempts.is_none()
121+
&& retry_overall_timeout_secs.is_none()
122+
&& !has_http_headers
123+
{
124+
return Ok(None);
125+
}
126+
127+
let mut http = databricks_sql_kernel::HttpConfig::default();
128+
if let Some(timeout) = request_timeout {
129+
http.request_timeout = timeout;
130+
}
131+
if let Some(s) = retry_min_wait_secs {
132+
http.retry_min_wait = std::time::Duration::from_secs(s);
133+
}
134+
if let Some(s) = retry_max_wait_secs {
135+
http.retry_max_wait = std::time::Duration::from_secs(s);
136+
}
137+
if let Some(attempts) = retry_max_attempts {
138+
http.retry_max_retries = attempts.saturating_sub(1);
139+
}
140+
if let Some(s) = retry_overall_timeout_secs {
141+
http.overall_timeout = std::time::Duration::from_secs(s);
142+
}
143+
if let Some(headers) = http_headers {
144+
http.custom_headers = headers;
145+
}
146+
Ok(Some(http))
147+
}
148+
98149
#[allow(clippy::too_many_arguments)]
99150
fn build_driver_system_configuration(
100151
driver_name: Option<String>,
@@ -183,6 +234,10 @@ impl Session {
183234
/// CloudFetch requests; token-endpoint (auth) calls keep a fixed
184235
/// fast-fail policy.
185236
///
237+
/// `request_timeout_secs` caps one HTTP request from connect through
238+
/// response-body completion. Unset or `0` keeps the kernel default
239+
/// (120 seconds).
240+
///
186241
/// `http_headers` is a list of `(name, value)` pairs sent on every
187242
/// request — used to forward the connector's caller headers (which
188243
/// also carry the composed `User-Agent`). The kernel applies them
@@ -223,6 +278,7 @@ impl Session {
223278
tls_skip_hostname_verify=false,
224279
tls_client_cert=None,
225280
tls_client_key=None,
281+
request_timeout_secs=None,
226282
retry_min_wait_secs=None,
227283
retry_max_wait_secs=None,
228284
retry_max_attempts=None,
@@ -279,6 +335,7 @@ impl Session {
279335
tls_skip_hostname_verify: bool,
280336
tls_client_cert: Option<Vec<u8>>,
281337
tls_client_key: Option<Vec<u8>>,
338+
request_timeout_secs: Option<f64>,
282339
retry_min_wait_secs: Option<u64>,
283340
retry_max_wait_secs: Option<u64>,
284341
retry_max_attempts: Option<u32>,
@@ -371,9 +428,10 @@ impl Session {
371428
None
372429
};
373430

374-
// Build an HttpConfig only when the caller tuned retry/backoff
375-
// or supplied custom headers; otherwise leave the builder on its
376-
// defaults (1s/60s/5-retries, no extra headers).
431+
// Build an HttpConfig only when the caller tuned the per-request
432+
// timeout, retry/backoff, or supplied custom headers; otherwise leave
433+
// the builder on its defaults (120s request timeout,
434+
// 1s/60s/5-retries, no extra headers).
377435
//
378436
// `retry_max_attempts` is the **total** number of attempts
379437
// (matching the connector's `_retry_stop_after_attempts_count`
@@ -387,33 +445,14 @@ impl Session {
387445
// connector forwards its `http_headers`, which also carries the
388446
// composed `User-Agent`). The kernel applies them before its own
389447
// `Authorization` / `x-databricks-org-id`, which always win.
390-
let has_http_headers = http_headers.as_ref().is_some_and(|h| !h.is_empty());
391-
let http_config = if retry_min_wait_secs.is_some()
392-
|| retry_max_wait_secs.is_some()
393-
|| retry_max_attempts.is_some()
394-
|| retry_overall_timeout_secs.is_some()
395-
|| has_http_headers
396-
{
397-
let mut http = databricks_sql_kernel::HttpConfig::default();
398-
if let Some(s) = retry_min_wait_secs {
399-
http.retry_min_wait = std::time::Duration::from_secs(s);
400-
}
401-
if let Some(s) = retry_max_wait_secs {
402-
http.retry_max_wait = std::time::Duration::from_secs(s);
403-
}
404-
if let Some(attempts) = retry_max_attempts {
405-
http.retry_max_retries = attempts.saturating_sub(1);
406-
}
407-
if let Some(s) = retry_overall_timeout_secs {
408-
http.overall_timeout = std::time::Duration::from_secs(s);
409-
}
410-
if let Some(headers) = http_headers {
411-
http.custom_headers = headers;
412-
}
413-
Some(http)
414-
} else {
415-
None
416-
};
448+
let http_config = build_http_config(
449+
request_timeout_secs,
450+
retry_min_wait_secs,
451+
retry_max_wait_secs,
452+
retry_max_attempts,
453+
retry_overall_timeout_secs,
454+
http_headers,
455+
)?;
417456

418457
let driver_system_configuration = build_driver_system_configuration(
419458
driver_name,
@@ -635,6 +674,27 @@ mod send_sync_tests {
635674
assert!(build_telemetry_config(false, None, None, None, None, Some(0)).is_err());
636675
}
637676

677+
#[test]
678+
fn request_timeout_kwarg_maps_to_http_config() {
679+
let default = databricks_sql_kernel::HttpConfig::default();
680+
assert!(build_http_config(None, None, None, None, None, None)
681+
.unwrap()
682+
.is_none());
683+
assert!(build_http_config(Some(0.0), None, None, None, None, None)
684+
.unwrap()
685+
.is_none());
686+
687+
let http = build_http_config(Some(0.5), None, None, None, None, None)
688+
.unwrap()
689+
.expect("positive request timeout should build an HTTP config");
690+
assert_eq!(http.request_timeout, std::time::Duration::from_millis(500));
691+
assert_eq!(http.retry_min_wait, default.retry_min_wait);
692+
assert_eq!(http.overall_timeout, default.overall_timeout);
693+
694+
assert!(build_http_config(Some(-1.0), None, None, None, None, None).is_err());
695+
assert!(build_http_config(Some(f64::NAN), None, None, None, None, None).is_err());
696+
}
697+
638698
#[test]
639699
fn driver_system_kwargs_map_to_kernel_config() {
640700
assert!(build_driver_system_configuration(

0 commit comments

Comments
 (0)