Replace repeated Mutex lock functions with a macro

This commit is contained in:
Ethan O'Brien
2024-11-02 12:09:35 -05:00
parent d9ce65df42
commit 7aaf22c923
6 changed files with 110 additions and 167 deletions

View File

@ -2,7 +2,6 @@ use json::{object, array, JsonValue};
use actix_web::{HttpRequest};
use rusqlite::params;
use std::sync::Mutex;
use std::thread;
use lazy_static::lazy_static;
use crate::encryption;
@ -167,42 +166,23 @@ fn get_json() -> JsonValue {
}
}
fn get_clearrate_json() -> JsonValue {
loop {
match CACHED_DATA.lock() {
Ok(mut result) => {
if result.is_none() {
result.replace(get_json());
}
let cache = result.as_ref().unwrap();
let rv = cache["cache"].clone();
if cache["last_updated"].as_u64().unwrap() + (60 * 60) < global::timestamp() {
thread::spawn(|| {
loop {
match CACHED_DATA.lock() {
Ok(mut result) => {
let new = get_json();
result.replace(new.clone());
break;
}
Err(_) => {
std::thread::sleep(std::time::Duration::from_millis(15));
}
}
}
});
}
return rv;
}
Err(_) => {
std::thread::sleep(std::time::Duration::from_millis(15));
}
}
async fn get_clearrate_json() -> JsonValue {
let mut result = crate::lock_onto_mutex!(CACHED_DATA);
if result.is_none() {
result.replace(get_json());
}
let cache = result.as_ref().unwrap();
let rv = cache["cache"].clone();
if cache["last_updated"].as_u64().unwrap() + (60 * 60) < global::timestamp() {
let mut result = crate::lock_onto_mutex!(CACHED_DATA);
let new = get_json();
result.replace(new.clone());
}
return rv;
}
pub fn clearrate(_req: HttpRequest) -> Option<JsonValue> {
Some(get_clearrate_json())
pub async fn clearrate(_req: HttpRequest) -> Option<JsonValue> {
Some(get_clearrate_json().await)
}
pub fn ranking(_req: HttpRequest, body: String) -> Option<JsonValue> {

View File

@ -219,10 +219,10 @@ fn get_rank(event: u32, user_id: u64) -> u32 {
0
}
pub fn ranking(_req: HttpRequest, body: String) -> Option<JsonValue> {
pub async fn ranking(_req: HttpRequest, body: String) -> Option<JsonValue> {
let body = &encryption::decrypt_packet(&body).unwrap();
let body: EventRankingGet = serde_json::from_str(body).unwrap();
let scores = crate::router::event_ranking::get_scores_json()[body.master_event_id.to_string()].clone();
let scores = crate::router::event_ranking::get_scores_json().await[body.master_event_id.to_string()].clone();
let mut rv = array![];
let mut i=1;
let start = if body.user_id == 0 { body.start_rank } else { get_rank(body.master_event_id, body.user_id) };

View File

@ -1,7 +1,6 @@
use json::{object, array, JsonValue};
use rusqlite::params;
use std::sync::Mutex;
use std::thread;
use lazy_static::lazy_static;
use crate::sql::SQLite;
@ -98,36 +97,17 @@ fn get_json() -> JsonValue {
}
}
pub fn get_scores_json() -> JsonValue {
loop {
match CACHED_DATA.lock() {
Ok(mut result) => {
if result.is_none() {
result.replace(get_json());
}
let cache = result.as_ref().unwrap();
let rv = cache["cache"].clone();
if cache["last_updated"].as_u64().unwrap() + (60 * 60) < global::timestamp() {
thread::spawn(|| {
loop {
match CACHED_DATA.lock() {
Ok(mut result) => {
let new = get_json();
result.replace(new.clone());
break;
}
Err(_) => {
std::thread::sleep(std::time::Duration::from_millis(15));
}
}
}
});
}
return rv;
}
Err(_) => {
std::thread::sleep(std::time::Duration::from_millis(15));
}
}
pub async fn get_scores_json() -> JsonValue {
let mut result = crate::lock_onto_mutex!(CACHED_DATA);
if result.is_none() {
result.replace(get_json());
}
let cache = result.as_ref().unwrap();
let rv = cache["cache"].clone();
if cache["last_updated"].as_u64().unwrap() + (60 * 60) < global::timestamp() {
let mut result = crate::lock_onto_mutex!(CACHED_DATA);
let new = get_json();
result.replace(new.clone());
}
return rv;
}