From 6e19246cb2a02f06d6e2ca3851d78b2f01f7bdb0 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Mon, 22 May 2023 13:07:37 +0300 Subject: [PATCH] fix: reduce the time to build user-agents --- Cargo.lock | 1 + Cargo.toml | 1 + src/search_results_handler/user_agent.rs | 20 +++++++++++--------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 85310e9..cec7cf7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3328,6 +3328,7 @@ dependencies = [ "handlebars", "log", "md5", + "once_cell", "rand 0.8.5", "redis", "reqwest 0.11.17", diff --git a/Cargo.toml b/Cargo.toml index ce99ca3..efe9435 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,3 +21,4 @@ rlua = {version="*"} redis = {version="*"} md5 = {version="*"} rand={version="*"} +once_cell = {version="*"} diff --git a/src/search_results_handler/user_agent.rs b/src/search_results_handler/user_agent.rs index 09dd684..13166bf 100644 --- a/src/search_results_handler/user_agent.rs +++ b/src/search_results_handler/user_agent.rs @@ -1,13 +1,8 @@ //! This module provides the functionality to generate random user agent string. -use fake_useragent::{Browsers, UserAgentsBuilder}; +use fake_useragent::{Browsers, UserAgents, UserAgentsBuilder}; -/// A function to generate random user agent to improve privacy of the user. -/// -/// # Returns -/// -/// A randomly generated user agent string. -pub fn random_user_agent() -> String { +static USER_AGENTS: once_cell::sync::Lazy = once_cell::sync::Lazy::new(|| { UserAgentsBuilder::new() .cache(false) .dir("/tmp") @@ -21,6 +16,13 @@ pub fn random_user_agent() -> String { .set_mozilla(), ) .build() - .random() - .to_string() +}); + +/// A function to generate random user agent to improve privacy of the user. +/// +/// # Returns +/// +/// A randomly generated user agent string. +pub fn random_user_agent() -> String { + USER_AGENTS.random().to_string() }