️ perf: rewrite the code by using a constant storing a prebuilt client globally for each thread (#384)

This commit is contained in:
neon_arch 2023-11-20 15:27:49 +03:00
parent 05bf05b0dd
commit b42adaa5a3
2 changed files with 20 additions and 6 deletions

View file

@ -3,7 +3,8 @@
use super::aggregation_models::SearchResult;
use error_stack::{Report, Result, ResultExt};
use std::{collections::HashMap, fmt, time::Duration};
use reqwest::Client;
use std::{collections::HashMap, fmt};
/// A custom error type used for handle engine associated errors.
#[derive(Debug)]
@ -71,12 +72,11 @@ pub trait SearchEngine: Sync + Send {
&self,
url: &str,
header_map: reqwest::header::HeaderMap,
request_timeout: u8,
client: &Client,
) -> Result<String, EngineError> {
// fetch the html from upstream search engine
Ok(reqwest::Client::new()
Ok(client
.get(url)
.timeout(Duration::from_secs(request_timeout as u64)) // Add timeout to request to avoid DDOSing the server
.headers(header_map) // add spoofed headers to emulate human behavior
.send()
.await
@ -109,7 +109,7 @@ pub trait SearchEngine: Sync + Send {
query: &str,
page: u32,
user_agent: &str,
request_timeout: u8,
client: &Client,
safe_search: u8,
) -> Result<HashMap<String, SearchResult>, EngineError>;
}