i hate programming :(((((((((( >_<

This commit is contained in:
Ethan O'Brien 2024-11-20 22:13:11 -06:00
parent 4d9991ce75
commit 7493724cd7
4 changed files with 88 additions and 94 deletions

32
Cargo.lock generated
View file

@ -28,7 +28,6 @@ dependencies = [
"actix-codec",
"actix-rt",
"actix-service",
"actix-tls",
"actix-utils",
"ahash",
"base64",
@ -122,25 +121,6 @@ dependencies = [
"pin-project-lite",
]
[[package]]
name = "actix-tls"
version = "3.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac453898d866cdbecdbc2334fe1738c747b4eba14a677261f2b768ba05329389"
dependencies = [
"actix-rt",
"actix-service",
"actix-utils",
"futures-core",
"impl-more",
"openssl",
"pin-project-lite",
"tokio",
"tokio-openssl",
"tokio-util",
"tracing",
]
[[package]]
name = "actix-utils"
version = "3.0.1"
@ -164,7 +144,6 @@ dependencies = [
"actix-rt",
"actix-server",
"actix-service",
"actix-tls",
"actix-utils",
"actix-web-codegen",
"ahash",
@ -2323,17 +2302,6 @@ dependencies = [
"tokio",
]
[[package]]
name = "tokio-openssl"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59df6849caa43bb7567f9a36f863c447d95a11d5903c9cc334ba32576a27eadd"
dependencies = [
"openssl",
"openssl-sys",
"tokio",
]
[[package]]
name = "tokio-rustls"
version = "0.26.0"

View file

@ -190,6 +190,7 @@ macro_rules! lock_onto_mutex {
break value;
}
Err(_) => {
$mutex.clear_poison();
actix_web::rt::time::sleep(std::time::Duration::from_millis(15)).await;
}
}

View file

@ -167,11 +167,13 @@ fn get_json() -> JsonValue {
}
async fn get_clearrate_json() -> JsonValue {
let cache = {
let mut result = crate::lock_onto_mutex!(CACHED_DATA);
if result.is_none() {
result.replace(get_json());
}
let cache = result.as_ref().unwrap();
result.as_ref().unwrap().clone()
};
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);

View file

@ -5,23 +5,8 @@ use json::{JsonValue, array};
use crate::router::clear_rate::Live;
pub struct SQLite {
engine: Mutex<Connection>
}
// This is duplicated, for ease of people wanting to use this file in their project
macro_rules! lock_onto_mutex {
($mutex:expr) => {{
loop {
match $mutex.lock() {
Ok(value) => {
break value;
}
Err(_) => {
std::thread::sleep(std::time::Duration::from_millis(10));
}
}
}
}};
engine: Mutex<Connection>,
sleep_duration: u64
}
impl SQLite {
@ -29,17 +14,30 @@ impl SQLite {
let conn = Connection::open(crate::get_data_path(path)).unwrap();
conn.execute("PRAGMA foreign_keys = ON;", ()).unwrap();
let instance = SQLite {
engine: Mutex::new(conn)
engine: Mutex::new(conn),
sleep_duration: 10
};
setup(&instance);
instance
}
pub fn lock_and_exec(&self, command: &str, args: &[&dyn ToSql]) {
let conn = lock_onto_mutex!(self.engine);
loop {
match self.engine.lock() {
Ok(conn) => {
conn.execute(command, args).unwrap();
return;
}
Err(_) => {
self.engine.clear_poison();
std::thread::sleep(std::time::Duration::from_millis(self.sleep_duration));
}
}
}
}
pub fn lock_and_select(&self, command: &str, args: &[&dyn ToSql]) -> Result<String, rusqlite::Error> {
let conn = lock_onto_mutex!(self.engine);
loop {
match self.engine.lock() {
Ok(conn) => {
let mut stmt = conn.prepare(command)?;
return stmt.query_row(args, |row| {
match row.get::<usize, i64>(0) {
@ -48,8 +46,17 @@ impl SQLite {
}
});
}
Err(_) => {
self.engine.clear_poison();
std::thread::sleep(std::time::Duration::from_millis(self.sleep_duration));
}
}
}
}
pub fn lock_and_select_all(&self, command: &str, args: &[&dyn ToSql]) -> Result<JsonValue, rusqlite::Error> {
let conn = lock_onto_mutex!(self.engine);
loop {
match self.engine.lock() {
Ok(conn) => {
let mut stmt = conn.prepare(command)?;
let map = stmt.query_map(args, |row| {
match row.get::<usize, i64>(0) {
@ -67,8 +74,17 @@ impl SQLite {
}
return Ok(rv);
}
Err(_) => {
self.engine.clear_poison();
std::thread::sleep(std::time::Duration::from_millis(self.sleep_duration));
}
}
}
}
pub fn get_live_data(&self, id: i64) -> Result<Live, rusqlite::Error> {
let conn = lock_onto_mutex!(self.engine);
loop {
match self.engine.lock() {
Ok(conn) => {
let mut stmt = conn.prepare("SELECT * FROM lives WHERE live_id=?1")?;
return stmt.query_row(params!(id), |row| {
Ok(Live {
@ -84,6 +100,13 @@ impl SQLite {
})
});
}
Err(_) => {
self.engine.clear_poison();
std::thread::sleep(std::time::Duration::from_millis(self.sleep_duration));
}
}
}
}
pub fn create_store_v2(&self, table: &str) {
self.lock_and_exec(table, params!());
}