mirror of
https://github.com/neon-mmd/websurfx.git
synced 2024-11-24 23:18:22 -05:00
Merge pull request #192 from neon-mmd/optimize-and-make-code-idiomatic
⚙️ Optimize and make code more idiomatic (part - III)
This commit is contained in:
commit
e581de3def
27
Cargo.lock
generated
27
Cargo.lock
generated
@ -300,6 +300,12 @@ version = "0.10.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341"
|
checksum = "619743e34b5ba4e9703bba34deac3427c72507c7159f5fd030aea8cac0cfe341"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "async-once-cell"
|
||||||
|
version = "0.5.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9338790e78aa95a416786ec8389546c4b6a1dfc3dc36071ed9518a9413a542eb"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "async-trait"
|
name = "async-trait"
|
||||||
version = "0.1.73"
|
version = "0.1.73"
|
||||||
@ -1601,6 +1607,16 @@ version = "0.2.147"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libmimalloc-sys"
|
||||||
|
version = "0.1.34"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "25d058a81af0d1c22d7a1c948576bee6d673f7af3c0f35564abd6c81122f513d"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.4.5"
|
version = "0.4.5"
|
||||||
@ -1729,6 +1745,15 @@ dependencies = [
|
|||||||
"autocfg 1.1.0",
|
"autocfg 1.1.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mimalloc"
|
||||||
|
version = "0.1.38"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "972e5f23f6716f62665760b0f4cbf592576a80c7b879ba9beaafc0e558894127"
|
||||||
|
dependencies = [
|
||||||
|
"libmimalloc-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mime"
|
name = "mime"
|
||||||
version = "0.3.17"
|
version = "0.3.17"
|
||||||
@ -3677,6 +3702,7 @@ dependencies = [
|
|||||||
"actix-cors",
|
"actix-cors",
|
||||||
"actix-files",
|
"actix-files",
|
||||||
"actix-web",
|
"actix-web",
|
||||||
|
"async-once-cell",
|
||||||
"async-trait",
|
"async-trait",
|
||||||
"criterion",
|
"criterion",
|
||||||
"dhat",
|
"dhat",
|
||||||
@ -3687,6 +3713,7 @@ dependencies = [
|
|||||||
"handlebars",
|
"handlebars",
|
||||||
"log",
|
"log",
|
||||||
"md5",
|
"md5",
|
||||||
|
"mimalloc",
|
||||||
"mlua",
|
"mlua",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
"rand 0.8.5",
|
"rand 0.8.5",
|
||||||
|
@ -27,9 +27,11 @@ once_cell = {version="1.18.0"}
|
|||||||
error-stack = {version="0.4.0"}
|
error-stack = {version="0.4.0"}
|
||||||
async-trait = {version="0.1.73"}
|
async-trait = {version="0.1.73"}
|
||||||
regex = {version="1.9.4", features=["perf"]}
|
regex = {version="1.9.4", features=["perf"]}
|
||||||
|
smallvec = {version="1.11.0", features=["union", "serde"]}
|
||||||
futures = {version="0.3.28"}
|
futures = {version="0.3.28"}
|
||||||
dhat = {version="0.3.2", optional = true}
|
dhat = {version="0.3.2", optional = true}
|
||||||
smallvec = {version="1.11.0", features=["union", "serde"]}
|
mimalloc = { version = "0.1.38", default-features = false }
|
||||||
|
async-once-cell = {version="0.5.3"}
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
rusty-hook = "^0.11.2"
|
rusty-hook = "^0.11.2"
|
||||||
@ -55,10 +57,10 @@ debug = false # This should only be commented when testing with dhat profiler
|
|||||||
split-debuginfo = '...'
|
split-debuginfo = '...'
|
||||||
debug-assertions = false
|
debug-assertions = false
|
||||||
overflow-checks = false
|
overflow-checks = false
|
||||||
lto = 'thin'
|
lto = true
|
||||||
panic = 'abort'
|
panic = 'abort'
|
||||||
incremental = false
|
incremental = false
|
||||||
codegen-units = 16
|
codegen-units = 1
|
||||||
rpath = false
|
rpath = false
|
||||||
strip = "debuginfo"
|
strip = "debuginfo"
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
//! This module contains the main function which handles the logging of the application to the
|
//! This module contains the main function which handles the logging of the application to the
|
||||||
//! stdout and handles the command line arguments provided and launches the `websurfx` server.
|
//! stdout and handles the command line arguments provided and launches the `websurfx` server.
|
||||||
|
|
||||||
|
use mimalloc::MiMalloc;
|
||||||
use std::net::TcpListener;
|
use std::net::TcpListener;
|
||||||
use websurfx::{config::parser::Config, run};
|
use websurfx::{config::parser::Config, run};
|
||||||
|
|
||||||
@ -11,6 +12,10 @@ use websurfx::{config::parser::Config, run};
|
|||||||
#[global_allocator]
|
#[global_allocator]
|
||||||
static ALLOC: dhat::Alloc = dhat::Alloc;
|
static ALLOC: dhat::Alloc = dhat::Alloc;
|
||||||
|
|
||||||
|
#[cfg(not(feature = "dhat-heap"))]
|
||||||
|
#[global_allocator]
|
||||||
|
static GLOBAL: MiMalloc = MiMalloc;
|
||||||
|
|
||||||
/// The function that launches the main server and registers all the routes of the website.
|
/// The function that launches the main server and registers all the routes of the website.
|
||||||
///
|
///
|
||||||
/// # Error
|
/// # Error
|
||||||
|
1
src/cache/cacher.rs
vendored
1
src/cache/cacher.rs
vendored
@ -17,6 +17,7 @@ use super::error::PoolError;
|
|||||||
/// * `pool_size` - It stores the size of the connection pool (in other words the number of
|
/// * `pool_size` - It stores the size of the connection pool (in other words the number of
|
||||||
/// connections that should be stored in the pool).
|
/// connections that should be stored in the pool).
|
||||||
/// * `current_connection` - It stores the index of which connection is being used at the moment.
|
/// * `current_connection` - It stores the index of which connection is being used at the moment.
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct RedisCache {
|
pub struct RedisCache {
|
||||||
connection_pool: Vec<ConnectionManager>,
|
connection_pool: Vec<ConnectionManager>,
|
||||||
pool_size: u8,
|
pool_size: u8,
|
||||||
|
@ -14,7 +14,12 @@ use crate::server::routes;
|
|||||||
|
|
||||||
use actix_cors::Cors;
|
use actix_cors::Cors;
|
||||||
use actix_files as fs;
|
use actix_files as fs;
|
||||||
use actix_web::{dev::Server, http::header, middleware::Logger, web, App, HttpServer};
|
use actix_web::{
|
||||||
|
dev::Server,
|
||||||
|
http::header,
|
||||||
|
middleware::{Compress, Logger},
|
||||||
|
web, App, HttpServer,
|
||||||
|
};
|
||||||
use config::parser::Config;
|
use config::parser::Config;
|
||||||
use handlebars::Handlebars;
|
use handlebars::Handlebars;
|
||||||
use handler::paths::{file_path, FileType};
|
use handler::paths::{file_path, FileType};
|
||||||
@ -68,6 +73,7 @@ pub fn run(listener: TcpListener, config: Config) -> std::io::Result<Server> {
|
|||||||
.app_data(web::Data::new(config.clone()))
|
.app_data(web::Data::new(config.clone()))
|
||||||
.wrap(cors)
|
.wrap(cors)
|
||||||
.wrap(Logger::default()) // added logging middleware for logging.
|
.wrap(Logger::default()) // added logging middleware for logging.
|
||||||
|
.wrap(Compress::default()) // compress request headers to reduce memory usage.
|
||||||
// Serve images and static files (css and js files).
|
// Serve images and static files (css and js files).
|
||||||
.service(
|
.service(
|
||||||
fs::Files::new("/static", format!("{}/static", public_folder_path))
|
fs::Files::new("/static", format!("{}/static", public_folder_path))
|
||||||
|
@ -16,6 +16,10 @@ use handlebars::Handlebars;
|
|||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use tokio::join;
|
use tokio::join;
|
||||||
|
|
||||||
|
// ---- Constants ----
|
||||||
|
/// Initialize redis cache connection once and store it on the heap.
|
||||||
|
const REDIS_CACHE: async_once_cell::OnceCell<RedisCache> = async_once_cell::OnceCell::new();
|
||||||
|
|
||||||
/// A named struct which deserializes all the user provided search parameters and stores them.
|
/// A named struct which deserializes all the user provided search parameters and stores them.
|
||||||
///
|
///
|
||||||
/// # Fields
|
/// # Fields
|
||||||
@ -158,10 +162,17 @@ async fn results(
|
|||||||
page: u32,
|
page: u32,
|
||||||
req: &HttpRequest,
|
req: &HttpRequest,
|
||||||
) -> Result<SearchResults, Box<dyn std::error::Error>> {
|
) -> Result<SearchResults, Box<dyn std::error::Error>> {
|
||||||
//Initialize redis cache connection struct
|
let redis_cache: RedisCache = REDIS_CACHE
|
||||||
let mut redis_cache = RedisCache::new(&config.redis_url, 5).await?;
|
.get_or_init(async {
|
||||||
|
// Initialize redis cache connection pool only one and store it in the heap.
|
||||||
|
RedisCache::new(&config.redis_url, 5).await.unwrap()
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.clone();
|
||||||
|
|
||||||
// fetch the cached results json.
|
// fetch the cached results json.
|
||||||
let cached_results_json = redis_cache.cached_json(&url).await;
|
let cached_results_json: Result<String, error_stack::Report<crate::cache::error::PoolError>> =
|
||||||
|
redis_cache.clone().cached_json(&url).await;
|
||||||
// check if fetched cache results was indeed fetched or it was an error and if so
|
// check if fetched cache results was indeed fetched or it was an error and if so
|
||||||
// handle the data accordingly.
|
// handle the data accordingly.
|
||||||
match cached_results_json {
|
match cached_results_json {
|
||||||
@ -206,6 +217,7 @@ async fn results(
|
|||||||
|
|
||||||
results.add_style(&config.style);
|
results.add_style(&config.style);
|
||||||
redis_cache
|
redis_cache
|
||||||
|
.clone()
|
||||||
.cache_results(&serde_json::to_string(&results)?, &url)
|
.cache_results(&serde_json::to_string(&results)?, &url)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(results)
|
Ok(results)
|
||||||
|
Loading…
Reference in New Issue
Block a user