From 548b3d66d74bd865e9d9688237779d182cd4e580 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Mon, 13 May 2024 13:16:42 +0200 Subject: [PATCH] Fix for reuse connections --- .../approved_bot/services/batch_downloader.rs | 9 +++++++-- .../approved_bot/services/book_cache/mod.rs | 11 +++++++---- .../approved_bot/services/book_library/mod.rs | 7 ++++++- .../approved_bot/services/user_settings/mod.rs | 17 +++++++++++------ src/bots_manager/bot_manager_client.rs | 11 +++++++---- 5 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/bots/approved_bot/services/batch_downloader.rs b/src/bots/approved_bot/services/batch_downloader.rs index 31cb2dd..d7064b9 100644 --- a/src/bots/approved_bot/services/batch_downloader.rs +++ b/src/bots/approved_bot/services/batch_downloader.rs @@ -1,3 +1,4 @@ +use once_cell::sync::Lazy; use smallvec::SmallVec; use smartstring::alias::String as SmartString; @@ -5,6 +6,10 @@ use serde::{Deserialize, Serialize}; use crate::config; + +pub static CLIENT: Lazy = Lazy::new(reqwest::Client::new); + + #[derive(Serialize, Deserialize)] #[serde(rename_all = "snake_case")] pub enum TaskObjectType { @@ -43,7 +48,7 @@ pub struct Task { pub async fn create_task( data: CreateTaskData, ) -> Result> { - Ok(reqwest::Client::new() + Ok(CLIENT .post(format!("{}/api/", &config::CONFIG.batch_downloader_url)) .body(serde_json::to_string(&data).unwrap()) .header("Authorization", &config::CONFIG.batch_downloader_api_key) @@ -56,7 +61,7 @@ pub async fn create_task( } pub async fn get_task(task_id: String) -> Result> { - Ok(reqwest::Client::new() + Ok(CLIENT .get(format!( "{}/api/check_archive/{task_id}", &config::CONFIG.batch_downloader_url diff --git a/src/bots/approved_bot/services/book_cache/mod.rs b/src/bots/approved_bot/services/book_cache/mod.rs index 82b262f..bcf6096 100644 --- a/src/bots/approved_bot/services/book_cache/mod.rs +++ b/src/bots/approved_bot/services/book_cache/mod.rs @@ -1,4 +1,5 @@ use base64::{engine::general_purpose, Engine}; +use once_cell::sync::Lazy; use reqwest::StatusCode; use crate::{bots::approved_bot::modules::download::callback_data::DownloadQueryData, bots_manager::BotCache, config}; @@ -8,6 +9,9 @@ use self::types::{CachedMessage, DownloadFile}; pub mod types; +pub static CLIENT: Lazy = Lazy::new(reqwest::Client::new); + + pub async fn get_cached_message( download_data: &DownloadQueryData, bot_cache: BotCache, @@ -19,8 +23,7 @@ pub async fn get_cached_message( let is_need_copy = bot_cache == BotCache::Cache; - let client = reqwest::Client::new(); - let response = client + let response = CLIENT .get(format!( "{}/api/v1/{id}/{format}/?copy={is_need_copy}", &config::CONFIG.cache_server_url @@ -46,7 +49,7 @@ pub async fn download_file( file_type: format, } = download_data; - let response = reqwest::Client::new() + let response = CLIENT .get(format!( "{}/api/v1/download/{id}/{format}/", &config::CONFIG.cache_server_url @@ -92,7 +95,7 @@ pub async fn download_file_by_link( filename: String, link: String, ) -> Result, Box> { - let response = reqwest::Client::new() + let response = CLIENT .get(link) .send() .await? diff --git a/src/bots/approved_bot/services/book_library/mod.rs b/src/bots/approved_bot/services/book_library/mod.rs index e9cb655..873fae4 100644 --- a/src/bots/approved_bot/services/book_library/mod.rs +++ b/src/bots/approved_bot/services/book_library/mod.rs @@ -1,6 +1,7 @@ pub mod formatters; pub mod types; +use once_cell::sync::Lazy; use smartstring::alias::String as SmartString; use serde::de::DeserializeOwned; @@ -11,6 +12,10 @@ use crate::config; use self::types::Empty; + +pub static CLIENT: Lazy = Lazy::new(reqwest::Client::new); + + fn get_allowed_langs_params( allowed_langs: SmallVec<[SmartString; 3]>, ) -> Vec<(&'static str, SmartString)> { @@ -27,7 +32,7 @@ async fn _make_request( where T: DeserializeOwned, { - let response = reqwest::Client::new() + let response = CLIENT .get(format!("{}{}", &config::CONFIG.book_server_url, url)) .query(¶ms) .header("Authorization", &config::CONFIG.book_server_api_key) diff --git a/src/bots/approved_bot/services/user_settings/mod.rs b/src/bots/approved_bot/services/user_settings/mod.rs index 7dfbe30..d5a9fbb 100644 --- a/src/bots/approved_bot/services/user_settings/mod.rs +++ b/src/bots/approved_bot/services/user_settings/mod.rs @@ -1,3 +1,4 @@ +use once_cell::sync::Lazy; use reqwest::StatusCode; use serde::Deserialize; use serde_json::json; @@ -8,6 +9,10 @@ use tracing::log; use crate::{bots_manager::USER_LANGS_CACHE, config}; + +pub static CLIENT: Lazy = Lazy::new(reqwest::Client::new); + + #[derive(Deserialize, Debug, Clone)] pub struct Lang { // pub id: u32, @@ -28,7 +33,7 @@ pub struct UserSettings { pub async fn get_user_settings( user_id: UserId, ) -> Result, Box> { - let response = reqwest::Client::new() + let response = CLIENT .get(format!( "{}/users/{}", &config::CONFIG.user_settings_url, @@ -88,7 +93,7 @@ pub async fn create_or_update_user_settings( "allowed_langs": allowed_langs.into_vec() }); - let response = reqwest::Client::new() + let response = CLIENT .post(format!("{}/users/", &config::CONFIG.user_settings_url)) .body(body.to_string()) .header("Authorization", &config::CONFIG.user_settings_api_key) @@ -101,7 +106,7 @@ pub async fn create_or_update_user_settings( } pub async fn get_langs() -> Result, Box> { - let response = reqwest::Client::new() + let response = CLIENT .get(format!("{}/languages/", &config::CONFIG.user_settings_url)) .header("Authorization", &config::CONFIG.user_settings_api_key) .send() @@ -114,7 +119,7 @@ pub async fn get_langs() -> Result, Box Result<(), Box> { - reqwest::Client::new() + CLIENT .post(format!( "{}/users/{user_id}/update_activity", &config::CONFIG.user_settings_url @@ -131,7 +136,7 @@ pub async fn is_need_donate_notifications( chat_id: ChatId, is_private: bool, ) -> Result> { - let response = reqwest::Client::new() + let response = CLIENT .get(format!( "{}/donate_notifications/{chat_id}/is_need_send?is_private={is_private}", &config::CONFIG.user_settings_url @@ -147,7 +152,7 @@ pub async fn is_need_donate_notifications( pub async fn mark_donate_notification_sent( chat_id: ChatId, ) -> Result<(), Box> { - reqwest::Client::new() + CLIENT .post(format!( "{}/donate_notifications/{chat_id}", &config::CONFIG.user_settings_url diff --git a/src/bots_manager/bot_manager_client.rs b/src/bots_manager/bot_manager_client.rs index 8a77f0f..85317a3 100644 --- a/src/bots_manager/bot_manager_client.rs +++ b/src/bots_manager/bot_manager_client.rs @@ -1,7 +1,12 @@ +use once_cell::sync::Lazy; use serde::Deserialize; use crate::config; + +pub static CLIENT: Lazy = Lazy::new(reqwest::Client::new); + + #[derive(Deserialize, Debug, PartialEq, Clone, Copy)] pub enum BotCache { #[serde(rename = "original")] @@ -20,8 +25,7 @@ pub struct BotData { } pub async fn get_bots() -> Result, reqwest::Error> { - let client = reqwest::Client::new(); - let response = client + let response = CLIENT .get(&config::CONFIG.manager_url) .header("Authorization", &config::CONFIG.manager_api_key) .send() @@ -35,8 +39,7 @@ pub async fn get_bots() -> Result, reqwest::Error> { pub async fn delete_bot(id: u32) -> Result<(), reqwest::Error> { - let client = reqwest::Client::new(); - let response = client + let response = CLIENT .delete(&format!("{}/{}/", config::CONFIG.manager_url, id)) .header("Authorization", &config::CONFIG.manager_api_key) .send()