use parking_lot mutex to ignore all poisoning
This commit is contained in:
parent
957e15d0d8
commit
1bb63b1fa4
3 changed files with 9 additions and 6 deletions
13
src/main.rs
13
src/main.rs
|
|
@ -1,4 +1,4 @@
|
|||
use std::sync::{Arc, Mutex};
|
||||
use std::sync::Arc;
|
||||
|
||||
use askama::Template;
|
||||
use axum::{
|
||||
|
|
@ -8,6 +8,7 @@ use axum::{
|
|||
response::Html,
|
||||
routing::{get, post},
|
||||
};
|
||||
use parking_lot::Mutex;
|
||||
use rusqlite::Connection;
|
||||
use tower_http::trace::TraceLayer;
|
||||
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> {
|
||||
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",
|
||||
|
|
@ -132,7 +133,7 @@ fn get_foods(conn: &Arc<Mutex<Connection>>) -> Vec<Food> {
|
|||
}
|
||||
|
||||
fn get_sum(conn: &Arc<Mutex<Connection>>) -> i32 {
|
||||
let conn = conn.lock().unwrap();
|
||||
let conn = conn.lock();
|
||||
let mut stmt = conn
|
||||
.prepare("SELECT SUM(kc_per_serving * actual_servings) as kc FROM food")
|
||||
.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) {
|
||||
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 new: i32 = stmt.query_one((id,), |row| row.get(0)).unwrap();
|
||||
debug!(id, new_serving_count = new, "increase");
|
||||
}
|
||||
|
||||
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 food = stmt
|
||||
.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) {
|
||||
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 new: i32 = stmt.query_one((id,), |row| row.get(0)).unwrap();
|
||||
debug!(id, new_serving_count = new, "decrease");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue