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.
This commit is contained in:
2026-01-30 19:25:15 +00:00
parent c66312ce56
commit 098dc4426f

View File

@@ -25,19 +25,24 @@ pub mod types;
#[derive(Clone)]
pub struct OllamaClient {
server_address: String,
client: reqwest::Client,
}
impl OllamaClient {
pub fn new<S: AsRef<str>>(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<VersionResponse> {
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<TagsResponse> {
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<PsResponse> {
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<Item = OllamaResult<T>> {
let client = reqwest::Client::new();
let client = self.client.clone();
Box::pin(stream! {
let response = client
.post(endpoint)