Skip to content
Merged
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
20 changes: 15 additions & 5 deletions devolutions-gateway/src/api/ai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! It handles authentication via a gateway API key and forwards requests to the
//! configured AI provider endpoints.

use std::sync::OnceLock;
use std::time::Duration;

use axum::Router;
Expand Down Expand Up @@ -50,12 +51,21 @@ pub fn make_router<S>(state: DgwState) -> Router<S> {
.with_state(state)
}

/// Default HTTP client with a longer timeout suitable for AI requests.
/// Returns a cached HTTP client with the specified timeout.
///
/// The client is created once on first use and cloned for subsequent calls (cloning a
/// reqwest::Client is cheap as it uses Arc internally).
fn create_client(timeout: Duration) -> reqwest::Client {
reqwest::Client::builder()
.timeout(timeout)
.build()
.expect("parameters known to be valid only")
static AI_CLIENT: OnceLock<reqwest::Client> = OnceLock::new();

AI_CLIENT
.get_or_init(|| {
reqwest::Client::builder()
.timeout(timeout)
.build()
.expect("parameters known to be valid only")
})
.clone()
}

/// Validates the Authorization header against the gateway API key.
Expand Down
Loading