use parking_lot mutex to ignore all poisoning

This commit is contained in:
Khaïs COLIN 2025-10-19 17:50:07 +02:00
parent 957e15d0d8
commit 1bb63b1fa4
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
3 changed files with 9 additions and 6 deletions

1
Cargo.lock generated
View file

@ -747,6 +747,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"askama", "askama",
"axum", "axum",
"parking_lot",
"rusqlite", "rusqlite",
"tokio", "tokio",
"tower-http", "tower-http",

View file

@ -6,6 +6,7 @@ edition = "2024"
[dependencies] [dependencies]
askama = "0.14.0" askama = "0.14.0"
axum = "0.8.6" axum = "0.8.6"
parking_lot = "0.12.5"
rusqlite = { version = "0.37.0", features = ["bundled"] } rusqlite = { version = "0.37.0", features = ["bundled"] }
tokio = { version = "1.48.0", features = ["full"] } tokio = { version = "1.48.0", features = ["full"] }
tower-http = { version = "0.6.6", features = ["trace"] } tower-http = { version = "0.6.6", features = ["trace"] }

View file

@ -1,4 +1,4 @@
use std::sync::{Arc, Mutex}; use std::sync::Arc;
use askama::Template; use askama::Template;
use axum::{ use axum::{
@ -8,6 +8,7 @@ use axum::{
response::Html, response::Html,
routing::{get, post}, routing::{get, post},
}; };
use parking_lot::Mutex;
use rusqlite::Connection; use rusqlite::Connection;
use tower_http::trace::TraceLayer; use tower_http::trace::TraceLayer;
use tower_request_id::{RequestId, RequestIdLayer}; use tower_request_id::{RequestId, RequestIdLayer};
@ -106,7 +107,7 @@ async fn main() -> Result<(), std::io::Error> {
} }
fn get_foods(conn: &Arc<Mutex<Connection>>) -> Vec<Food> { fn get_foods(conn: &Arc<Mutex<Connection>>) -> Vec<Food> {
let conn = conn.lock().unwrap(); let conn = conn.lock();
let mut stmt = conn let mut stmt = conn
.prepare( .prepare(
"SELECT id, portion, name, kc_per_serving, target_servings, actual_servings, color FROM food", "SELECT id, portion, name, kc_per_serving, target_servings, actual_servings, color FROM food",
@ -132,7 +133,7 @@ fn get_foods(conn: &Arc<Mutex<Connection>>) -> Vec<Food> {
} }
fn get_sum(conn: &Arc<Mutex<Connection>>) -> i32 { fn get_sum(conn: &Arc<Mutex<Connection>>) -> i32 {
let conn = conn.lock().unwrap(); let conn = conn.lock();
let mut stmt = conn let mut stmt = conn
.prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food") .prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food")
.unwrap(); .unwrap();
@ -149,14 +150,14 @@ async fn root(State(conn): State<Arc<Mutex<Connection>>>) -> Html<String> {
} }
fn do_increase(conn: &Arc<Mutex<Connection>>, id: i32) { fn do_increase(conn: &Arc<Mutex<Connection>>, id: i32) {
let conn = conn.lock().unwrap(); let conn = conn.lock();
let mut stmt = conn.prepare("UPDATE food SET actual_servings = (SELECT actual_servings FROM food WHERE id = ?1) + 1 WHERE id = ?1 RETURNING actual_servings").unwrap(); let mut stmt = conn.prepare("UPDATE food SET actual_servings = (SELECT actual_servings FROM food WHERE id = ?1) + 1 WHERE id = ?1 RETURNING actual_servings").unwrap();
let new: i32 = stmt.query_one((id,), |row| row.get(0)).unwrap(); let new: i32 = stmt.query_one((id,), |row| row.get(0)).unwrap();
debug!(id, new_serving_count = new, "increase"); debug!(id, new_serving_count = new, "increase");
} }
fn get_food(conn: &Arc<Mutex<Connection>>, id: i32) -> Food { fn get_food(conn: &Arc<Mutex<Connection>>, id: i32) -> Food {
let conn = conn.lock().unwrap(); let conn = conn.lock();
let mut stmt = conn.prepare("SELECT id, portion, name, kc_per_serving, target_servings, actual_servings, color FROM food WHERE id = ?1").unwrap(); let mut stmt = conn.prepare("SELECT id, portion, name, kc_per_serving, target_servings, actual_servings, color FROM food WHERE id = ?1").unwrap();
let food = stmt let food = stmt
.query_one((id,), |row| { .query_one((id,), |row| {
@ -184,7 +185,7 @@ async fn increase(State(conn): State<Arc<Mutex<Connection>>>, Path(id): Path<i32
} }
fn do_decrease(conn: &Arc<Mutex<Connection>>, id: i32) { fn do_decrease(conn: &Arc<Mutex<Connection>>, id: i32) {
let conn = conn.lock().unwrap(); let conn = conn.lock();
let mut stmt = conn.prepare("UPDATE food SET actual_servings = MAX((SELECT actual_servings FROM food WHERE id = ?1) - 1, 0) WHERE id = ?1 RETURNING actual_servings").unwrap(); let mut stmt = conn.prepare("UPDATE food SET actual_servings = MAX((SELECT actual_servings FROM food WHERE id = ?1) - 1, 0) WHERE id = ?1 RETURNING actual_servings").unwrap();
let new: i32 = stmt.query_one((id,), |row| row.get(0)).unwrap(); let new: i32 = stmt.query_one((id,), |row| row.get(0)).unwrap();
debug!(id, new_serving_count = new, "decrease"); debug!(id, new_serving_count = new, "decrease");