From 098dc4426f29daa3cee3ff1cdff2a2dde73da2be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cipriani=20Bandarra?= Date: Fri, 30 Jan 2026 19:25:15 +0000 Subject: [PATCH] Reuse reqwest::Client across requests for connection pooling Store a shared reqwest::Client on OllamaClient instead of creating a new one per request. Previously, version(), tags(), and ps() each used reqwest::get() which allocates a one-shot client, and stream_response() called reqwest::Client::new() on every invocation. Since reqwest::Client manages an internal connection pool, reusing it enables TCP and TLS connection reuse across calls. Cloning the client is cheap as it is Arc-backed internally. --- src/lib.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 836fff5..85da3e0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,19 +25,24 @@ pub mod types; #[derive(Clone)] pub struct OllamaClient { server_address: String, + client: reqwest::Client, } impl OllamaClient { pub fn new>(server_address: S) -> Self { Self { server_address: server_address.as_ref().to_string(), + client: reqwest::Client::new(), } } /// Retrieve the version of the Ollama pub async fn version(&self) -> OllamaResult { let request_address = format!("{}/api/version", self.server_address); - Ok(reqwest::get(request_address) + Ok(self + .client + .get(request_address) + .send() .await? .error_for_status()? .json() @@ -48,7 +53,10 @@ impl OllamaClient { pub async fn tags(&self) -> OllamaResult { let request_address = format!("{}/api/tags", self.server_address); info!("List models: {}", request_address); - Ok(reqwest::get(request_address) + Ok(self + .client + .get(request_address) + .send() .await? .error_for_status()? .json() @@ -59,7 +67,10 @@ impl OllamaClient { pub async fn ps(&self) -> OllamaResult { let request_address = format!("{}/api/ps", self.server_address); info!("List models: {}", request_address); - Ok(reqwest::get(request_address) + Ok(self + .client + .get(request_address) + .send() .await? .error_for_status()? .json() @@ -71,7 +82,7 @@ impl OllamaClient { endpoint: String, request: R, ) -> impl Stream> { - let client = reqwest::Client::new(); + let client = self.client.clone(); Box::pin(stream! { let response = client .post(endpoint)