Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 27, 2025

The AI router was creating a new reqwest::Client for every request. Since configuration is static at runtime, we can cache the client and reuse it.

  • Cache HTTP client in a function-local OnceLock, initialized on first use
  • Return cloned client (cheap, uses Arc internally)
fn create_client(timeout: Duration) -> reqwest::Client {
    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()
}
Original prompt

In devolutions-gateway/src/api/ai.rs, I think it’s better to reuse the reqwest client instead of creating a new one for each request. We could use some form of caching using a OnceLock or LazyLock. Currently, the configuration can’t be changed dynamically, so it’s fine to create once and forget about it. The reqwest::Client can be cloned (it uses Arc internally), so it’s possible to keep the same signature for the create_client function.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits November 27, 2025 20:47
Co-authored-by: CBenoit <3809077+CBenoit@users.noreply.github.com>
…tion

Co-authored-by: CBenoit <3809077+CBenoit@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor reqwest client to use cached instance refactor(dgw): reuse reqwest::Client via OnceLock in AI router Nov 27, 2025
Copilot AI requested a review from CBenoit November 27, 2025 20:54
Copilot finished work on behalf of CBenoit November 27, 2025 20:54
@CBenoit CBenoit marked this pull request as ready for review November 27, 2025 21:33
@CBenoit CBenoit enabled auto-merge (squash) November 27, 2025 21:33
@CBenoit CBenoit merged commit 0b37f40 into master Nov 27, 2025
73 of 80 checks passed
@CBenoit CBenoit deleted the copilot/refactor-reqwest-client-usage branch November 27, 2025 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants