File tree Expand file tree Collapse file tree 1 file changed +15
-5
lines changed
devolutions-gateway/src/api Expand file tree Collapse file tree 1 file changed +15
-5
lines changed Original file line number Diff line number Diff line change 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 ;
89use std:: time:: Duration ;
910
1011use 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).
5458fn 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.
You can’t perform that action at this time.
0 commit comments