Skip to content

Commit 0b37f40

Browse files
CopilotCBenoit
andauthored
refactor(dgw): reuse reqwest::Client via OnceLock in AI router (#1593)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: CBenoit <[email protected]>
1 parent 8e9ed60 commit 0b37f40

File tree

1 file changed

+15
-5
lines changed
  • devolutions-gateway/src/api

1 file changed

+15
-5
lines changed

devolutions-gateway/src/api/ai.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
//! It handles authentication via a gateway API key and forwards requests to the
66
//! configured AI provider endpoints.
77
8+
use std::sync::OnceLock;
89
use std::time::Duration;
910

1011
use axum::Router;
@@ -50,12 +51,21 @@ pub fn make_router<S>(state: DgwState) -> Router<S> {
5051
.with_state(state)
5152
}
5253

53-
/// Default HTTP client with a longer timeout suitable for AI requests.
54+
/// Returns a cached HTTP client with the specified timeout.
55+
///
56+
/// The client is created once on first use and cloned for subsequent calls (cloning a
57+
/// reqwest::Client is cheap as it uses Arc internally).
5458
fn create_client(timeout: Duration) -> reqwest::Client {
55-
reqwest::Client::builder()
56-
.timeout(timeout)
57-
.build()
58-
.expect("parameters known to be valid only")
59+
static AI_CLIENT: OnceLock<reqwest::Client> = OnceLock::new();
60+
61+
AI_CLIENT
62+
.get_or_init(|| {
63+
reqwest::Client::builder()
64+
.timeout(timeout)
65+
.build()
66+
.expect("parameters known to be valid only")
67+
})
68+
.clone()
5969
}
6070

6171
/// Validates the Authorization header against the gateway API key.

0 commit comments

Comments
 (0)