From abda1529e4bfafa9255e6460353d218bd3c048e3 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Tue, 15 Oct 2024 17:47:17 -0500 Subject: fix(http): use LazyLock for reqwest::Client --- src/service/http.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/service/http.rs b/src/service/http.rs index 5b0fdd5..ce9a7e4 100644 --- a/src/service/http.rs +++ b/src/service/http.rs @@ -1,7 +1,6 @@ -use std::{fmt::Display, time::Duration}; +use std::{fmt::Display, sync::LazyLock, time::Duration}; use axum::http::status::StatusCode; -use reqwest::Client; use serde::{Deserialize, Serialize}; use url::Url; @@ -9,6 +8,8 @@ use crate::status::Sender; use super::IntoService; +static CLIENT: LazyLock = LazyLock::new(reqwest::Client::new); + #[derive(Debug, thiserror::Error)] pub enum Error { #[error("Request error: {0}")] @@ -24,18 +25,13 @@ pub struct Http { pub method: Method, #[serde(default, with = "status_code")] pub status_code: StatusCode, - #[serde(skip, default)] - pub client: Option, #[serde(default = "super::default_interval")] pub interval: Duration, } impl Http { async fn check(&self) -> Result<(), Error> { - let client = match self.client.as_ref() { - Some(client) => client, - None => &Client::new(), - }; + let client = CLIENT.clone(); let req = client .request(self.method.into(), self.url.clone()) .build()?; -- cgit v1.2.3-70-g09d2