Skip to main content

adk_model/
http_timeouts.rs

1//! Connect and idle timeouts for the HTTP transport of model clients.
2
3use std::time::Duration;
4
5/// Connect and idle timeouts for a model client's HTTP transport.
6///
7/// | Bound | Covers |
8/// |-------|--------|
9/// | `connect` | establishing the connection, TLS included |
10/// | `idle` | each wait for data: the response headers, then every gap between body chunks of a streamed or complete response |
11///
12/// A request that gets no response headers within `idle`, a stream that stops sending data
13/// for `idle`, and a complete response whose body stalls for `idle` all fail with
14/// `ErrorCategory::Timeout` instead of holding the run, as does a connection not established
15/// within `connect`; a long stream that keeps sending data is never cut off. The defaults are
16/// 30 seconds to connect and 10 minutes idle, the request timeout of the official OpenAI and
17/// Anthropic SDKs, which leaves room for reasoning models that think for minutes before
18/// their first token. The OpenAI-compatible, OpenAI Responses, Azure OpenAI, Azure AI, Groq,
19/// DeepSeek, OpenRouter, Ollama, and Bedrock clients apply them by default and take others
20/// through `with_timeouts`; the Gemini API-key client applies them by default and takes
21/// others through `GeminiModel::new_with_base_url_and_timeouts`. Bedrock applies them as the SDK's connect and read timeouts plus
22/// a bound on each stream event; an SDK client passed to `BedrockClient::from_client` keeps
23/// its own connect and read timeouts until `with_timeouts` replaces them.
24///
25/// # Example
26///
27/// ```
28/// use std::time::Duration;
29///
30/// use adk_model::HttpTimeouts;
31///
32/// // Fail a stream that sends nothing for two minutes; pass to a client's `with_timeouts`.
33/// let timeouts = HttpTimeouts::new(Duration::from_secs(10), Duration::from_secs(120));
34/// assert_eq!(timeouts.idle, Duration::from_secs(120));
35/// assert_eq!(HttpTimeouts::default(), HttpTimeouts::new(Duration::from_secs(30), Duration::from_secs(600)));
36/// ```
37#[derive(Debug, Clone, Copy, PartialEq, Eq)]
38pub struct HttpTimeouts {
39    /// Bound on establishing a connection.
40    pub connect: Duration,
41    /// Bound on each wait for response data, headers included.
42    pub idle: Duration,
43}
44
45impl HttpTimeouts {
46    /// Timeouts with the given connect and idle bounds.
47    pub const fn new(connect: Duration, idle: Duration) -> Self {
48        Self { connect, idle }
49    }
50}
51
52impl Default for HttpTimeouts {
53    fn default() -> Self {
54        Self::new(Duration::from_secs(30), Duration::from_secs(600))
55    }
56}
57
58#[cfg(any(
59    feature = "openai",
60    feature = "gemini",
61    feature = "groq",
62    feature = "muse",
63    feature = "deepseek",
64    feature = "azure-ai",
65    feature = "ollama",
66    feature = "openrouter"
67))]
68impl HttpTimeouts {
69    /// Applies both bounds to a `reqwest` client builder.
70    pub(crate) fn apply(self, builder: reqwest::ClientBuilder) -> reqwest::ClientBuilder {
71        builder.connect_timeout(self.connect).read_timeout(self.idle)
72    }
73}
74
75/// The category of a transport failure: `Timeout` when a bound expired, before the response
76/// headers or between body chunks, otherwise `Unavailable`.
77#[cfg(any(
78    feature = "openai",
79    feature = "gemini",
80    feature = "groq",
81    feature = "muse",
82    feature = "deepseek",
83    feature = "azure-ai",
84    feature = "openrouter"
85))]
86pub(crate) fn transport_category(error: &reqwest::Error) -> adk_core::ErrorCategory {
87    if error.is_timeout() {
88        adk_core::ErrorCategory::Timeout
89    } else {
90        adk_core::ErrorCategory::Unavailable
91    }
92}