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:
19
src/lib.rs
19
src/lib.rs
@@ -25,19 +25,24 @@ pub mod types;
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct OllamaClient {
|
pub struct OllamaClient {
|
||||||
server_address: String,
|
server_address: String,
|
||||||
|
client: reqwest::Client,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OllamaClient {
|
impl OllamaClient {
|
||||||
pub fn new<S: AsRef<str>>(server_address: S) -> Self {
|
pub fn new<S: AsRef<str>>(server_address: S) -> Self {
|
||||||
Self {
|
Self {
|
||||||
server_address: server_address.as_ref().to_string(),
|
server_address: server_address.as_ref().to_string(),
|
||||||
|
client: reqwest::Client::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Retrieve the version of the Ollama
|
/// Retrieve the version of the Ollama
|
||||||
pub async fn version(&self) -> OllamaResult<VersionResponse> {
|
pub async fn version(&self) -> OllamaResult<VersionResponse> {
|
||||||
let request_address = format!("{}/api/version", self.server_address);
|
let request_address = format!("{}/api/version", self.server_address);
|
||||||
Ok(reqwest::get(request_address)
|
Ok(self
|
||||||
|
.client
|
||||||
|
.get(request_address)
|
||||||
|
.send()
|
||||||
.await?
|
.await?
|
||||||
.error_for_status()?
|
.error_for_status()?
|
||||||
.json()
|
.json()
|
||||||
@@ -48,7 +53,10 @@ impl OllamaClient {
|
|||||||
pub async fn tags(&self) -> OllamaResult<TagsResponse> {
|
pub async fn tags(&self) -> OllamaResult<TagsResponse> {
|
||||||
let request_address = format!("{}/api/tags", self.server_address);
|
let request_address = format!("{}/api/tags", self.server_address);
|
||||||
info!("List models: {}", request_address);
|
info!("List models: {}", request_address);
|
||||||
Ok(reqwest::get(request_address)
|
Ok(self
|
||||||
|
.client
|
||||||
|
.get(request_address)
|
||||||
|
.send()
|
||||||
.await?
|
.await?
|
||||||
.error_for_status()?
|
.error_for_status()?
|
||||||
.json()
|
.json()
|
||||||
@@ -59,7 +67,10 @@ impl OllamaClient {
|
|||||||
pub async fn ps(&self) -> OllamaResult<PsResponse> {
|
pub async fn ps(&self) -> OllamaResult<PsResponse> {
|
||||||
let request_address = format!("{}/api/ps", self.server_address);
|
let request_address = format!("{}/api/ps", self.server_address);
|
||||||
info!("List models: {}", request_address);
|
info!("List models: {}", request_address);
|
||||||
Ok(reqwest::get(request_address)
|
Ok(self
|
||||||
|
.client
|
||||||
|
.get(request_address)
|
||||||
|
.send()
|
||||||
.await?
|
.await?
|
||||||
.error_for_status()?
|
.error_for_status()?
|
||||||
.json()
|
.json()
|
||||||
@@ -71,7 +82,7 @@ impl OllamaClient {
|
|||||||
endpoint: String,
|
endpoint: String,
|
||||||
request: R,
|
request: R,
|
||||||
) -> impl Stream<Item = OllamaResult<T>> {
|
) -> impl Stream<Item = OllamaResult<T>> {
|
||||||
let client = reqwest::Client::new();
|
let client = self.client.clone();
|
||||||
Box::pin(stream! {
|
Box::pin(stream! {
|
||||||
let response = client
|
let response = client
|
||||||
.post(endpoint)
|
.post(endpoint)
|
||||||
|
|||||||
Reference in New Issue
Block a user