From d8a531cf979c6ea15ee352c4b9939d96c283beff Mon Sep 17 00:00:00 2001 From: Ana Alexandru-Gabriel Date: Fri, 12 Jun 2026 19:57:26 +0300 Subject: [PATCH 1/2] moving away from std sync --- Cargo.toml | 1 + src/main.rs | 6 ++-- src/metrics/prelude.rs | 70 +++++++++++++++++++++----------------- src/security/structures.rs | 6 ++-- 4 files changed, 46 insertions(+), 37 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5d978e2..2f1528b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ function_name = "0.3.0" bitflags = { version = "2",features = ["serde"]} prometheus = { version = "0.14.0", features = ["process"] } futures = "0.3.32" +tokio = { version = "1.51.0", features = ["sync"] } # Improves comp time ? [profile.dev] diff --git a/src/main.rs b/src/main.rs index f3c69cc..4019689 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,12 +32,12 @@ async fn main() -> Result<(), Box> { if let Ok(ssesh) = Box::pin(db::prelude::new_scylla_session(&format!("{scylla_inet}:9042"))).await { let mcache = db::prelude::new_moka_cache(1_000); let session = actix_web::web::Data::new(security::structures::ScyllaSession { - lock: std::sync::Mutex::new(ssesh) + lock: tokio::sync::Mutex::new(ssesh) }); let cache = actix_web::web::Data::new(security::structures::MokaCache { - lock: std::sync::Mutex::new(mcache) + lock: tokio::sync::Mutex::new(mcache) }); let rl_config = RateLimitConfig::default().max_requests(API_RATELIMIT_COUNT).window_secs(API_RATELIMIT_WINDOW_SECONDS); @@ -124,4 +124,4 @@ async fn main() -> Result<(), Box> { } Ok(()) } - + diff --git a/src/metrics/prelude.rs b/src/metrics/prelude.rs index a1a1b56..1b24bed 100644 --- a/src/metrics/prelude.rs +++ b/src/metrics/prelude.rs @@ -1,10 +1,10 @@ -use actix_web::{web::Data, Error}; use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform}; -use futures::future::{ok, Ready}; -use prometheus::{HistogramVec, HistogramOpts, IntCounterVec, IntCounter, Opts, Registry}; +use actix_web::{Error, web::Data}; +use futures::future::{Ready, ok}; +use prometheus::{HistogramOpts, HistogramVec, IntCounter, IntCounterVec, Opts, Registry}; +use std::pin::Pin; use std::task::{Context, Poll}; use std::time::Instant; -use std::pin::Pin; #[derive(Clone)] pub struct MetricsCollector { @@ -19,27 +19,28 @@ impl MetricsCollector { pub fn new(registry: &Registry) -> Result { let request_counter = IntCounterVec::new( Opts::new("http_requests_total", "Total number of HTTP requests"), - &["method", "endpoint", "status"] + &["method", "endpoint", "status"], )?; - + let response_time_histogram = HistogramVec::new( - HistogramOpts::new("http_request_duration_seconds", "HTTP request duration in seconds"), - &["method", "endpoint"] + HistogramOpts::new( + "http_request_duration_seconds", + "HTTP request duration in seconds", + ), + &["method", "endpoint"], )?; - + let request_size = IntCounterVec::new( Opts::new("http_request_size_bytes", "HTTP request size in bytes"), - &["method", "endpoint"] + &["method", "endpoint"], )?; - let total_cache_hit_count = IntCounter::new( - "total_cache_hit_count", "How many cache hits happened." - )?; + let total_cache_hit_count = + IntCounter::new("total_cache_hit_count", "How many cache hits happened.")?; + + let total_cache_miss_count = + IntCounter::new("total_cache_miss_count", "How many cache misses happened.")?; - let total_cache_miss_count = IntCounter::new( - "total_cache_miss_count", "How many cache misses happened." - )?; - registry.register(Box::new(request_counter.clone()))?; registry.register(Box::new(total_cache_hit_count.clone()))?; registry.register(Box::new(total_cache_miss_count.clone()))?; @@ -113,7 +114,6 @@ pub struct MetricsMiddlewareService { collector: Data, } - /// To actually be able to interact with services asynchronously /// we need to implement this `actix_web` trait: /// @@ -138,14 +138,16 @@ where fn call(&self, req: ServiceRequest) -> Self::Future { let start_time = Instant::now(); let collector = self.collector.clone(); - + let method: String = req.method().to_string(); let endpoint = MetricsCollector::get_endpoint_pattern(&req).clone(); - + if let Some(content_length) = req.headers().get("content-length") - && let Ok(size_str) = content_length.to_str() - && let Ok(size) = size_str.parse::() { - collector.request_size + && let Ok(size_str) = content_length.to_str() + && let Ok(size) = size_str.parse::() + { + collector + .request_size .with_label_values(&[&method, &endpoint]) .inc_by(size); } @@ -177,29 +179,33 @@ where // counter for requests total, etc.) let result = fut.await; let duration = start_time.elapsed().as_secs_f64(); - + match result { Ok(response) => { - collector.response_time_histogram + collector + .response_time_histogram .with_label_values(&[&method, &endpoint]) .observe(duration); - + let status = response.status().as_u16().to_string(); - collector.request_counter + collector + .request_counter .with_label_values(&[&method, &endpoint, &status]) .inc(); Ok(response) - }, + } Err(e) => { - collector.response_time_histogram + collector + .response_time_histogram .with_label_values(&[&method, &endpoint]) .observe(duration); - - collector.request_counter + + collector + .request_counter .with_label_values(&[&method, &endpoint, &"error".to_string()]) .inc(); - + Err(e) } } diff --git a/src/security/structures.rs b/src/security/structures.rs index 8cc13dc..69027f0 100644 --- a/src/security/structures.rs +++ b/src/security/structures.rs @@ -1,7 +1,9 @@ +use tokio; + pub struct ScyllaSession { - pub lock: std::sync::Mutex + pub lock: tokio::sync::Mutex, } pub struct MokaCache { - pub lock: std::sync::Mutex> + pub lock: tokio::sync::Mutex>, } From 9094d6c4727d2b67ac652ab862d4d3e197e3e4fa Mon Sep 17 00:00:00 2001 From: Ana Alexandru-Gabriel Date: Fri, 12 Jun 2026 20:00:04 +0300 Subject: [PATCH 2/2] re-enabling awaiting_holding_lock lint check --- test-env-compose/Dockerfile.test | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-env-compose/Dockerfile.test b/test-env-compose/Dockerfile.test index 022aaf7..67e206d 100644 --- a/test-env-compose/Dockerfile.test +++ b/test-env-compose/Dockerfile.test @@ -25,7 +25,7 @@ FROM workspace AS linter RUN --mount=type=cache,target=/usr/local/cargo/registry \ <