Skip to content

Commit

Permalink
begin work on aneri-http, rework on aneri-sql (+ dm api improvements)
Browse files Browse the repository at this point in the history
  • Loading branch information
Absolucy committed Sep 30, 2024
1 parent 03161be commit 6cc86f5
Show file tree
Hide file tree
Showing 16 changed files with 263 additions and 601 deletions.
427 changes: 222 additions & 205 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion crates/aneri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ aneri-crypto = { path = "../crypto", optional = true }
aneri-dmi = { path = "../dmi", optional = true }
aneri-encode = { path = "../encode", optional = true }
aneri-file = { path = "../file", optional = true }
aneri-http = { path = "../http", optional = true }
aneri-logger = { path = "../logger", optional = true }
aneri-rand = { path = "../rand", optional = true }
aneri-regex = { path = "../regex", optional = true }
Expand All @@ -33,11 +34,12 @@ bosion = "1"
[features]
default = ["all"]
# sub-crates
all = ["crypto", "dmi", "encode", "file", "logger", "rand", "regex", "sort", "time", "util"]
all = ["crypto", "dmi", "encode", "file", "http", "logger", "rand", "regex", "sort", "time", "util"]
crypto = ["aneri-crypto"]
dmi = ["aneri-dmi"]
encode = ["aneri-encode"]
file = ["aneri-file"]
http = ["aneri-http"]
logger = ["aneri-logger"]
rand = ["aneri-rand"]
regex = ["aneri-regex"]
Expand Down
2 changes: 2 additions & 0 deletions crates/aneri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub use aneri_dmi;
pub use aneri_encode;
#[cfg(feature = "file")]
pub use aneri_file;
#[cfg(feature = "http")]
pub use aneri_http;
#[cfg(feature = "logger")]
pub use aneri_logger;
#[cfg(feature = "rand")]
Expand Down
18 changes: 18 additions & 0 deletions crates/http/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "aneri-http"
version.workspace = true
authors.workspace = true
edition.workspace = true
rust-version.workspace = true
repository.workspace = true
license.workspace = true
publish.workspace = true

[dependencies]
meowtonin = { workspace = true }
ureq = { version = "3.0.0-rc1", default-features = false, features = ["gzip", "brotli"] }

[features]
default = ["rustls"]
rustls = ["ureq/rustls"]
native-tls = ["ureq/native-tls"]
Empty file added crates/http/src/lib.rs
Empty file.
10 changes: 5 additions & 5 deletions crates/sql/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ publish.workspace = true

[dependencies]
ahash = "0.8"
aneri-core = { path = "../core", features = ["runtime"] }
aneri-core = { path = "../core" }
meowtonin = { workspace = true }
meowtonin-serde = { workspace = true }
mysql_async = { version = "0.34", default-features = false, features = ["minimal"] }
mysql = { git = "https://github.com/ZeWaka/rust-mysql-simple.git", tag = "v25.0.1", default-features = false, optional = true }
scopeguard = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand All @@ -22,6 +22,6 @@ crossbeam-channel = "0.5"
static_init = "1"

[features]
default = ["native-tls"]
native-tls = ["mysql_async/default"]
rustls = ["mysql_async/default-rustls"]
default = ["rustls"]
rustls = ["mysql/default-rustls"]
native-tls = ["mysql/default"]
108 changes: 0 additions & 108 deletions crates/sql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,111 +9,3 @@
)]
#[macro_use]
extern crate meowtonin;
#[macro_use]
extern crate static_init;

pub mod opts;
pub mod pool;
pub mod query;
pub mod util;

use ahash::AHashMap;
use aneri_core::{ByondSlotKey, RUNTIME};
use crossbeam_channel::{Receiver, TryRecvError};
use meowtonin::{ByondError, ByondResult, ByondValue, ToByond};
use mysql_async::Params as SqlParams;
use slotmap::SlotMap;
use std::{
collections::hash_map::Entry,
sync::atomic::{AtomicUsize, Ordering},
thread,
};

static NEXT_JOB_ID: AtomicUsize = AtomicUsize::new(0);

struct Job {
rx: Receiver<Result<query::QueryResult, String>>,
handle: thread::JoinHandle<()>,
}

#[byond_fn]
pub fn sql_query_blocking(
handle: ByondSlotKey,
query: String,
params: Option<ByondValue>,
) -> ByondResult<ByondValue> {
let pool = match pool::get_conn(handle).expect("failed to get connection") {
Some(pool) => pool,
None => unimplemented!("pool offline"),
};
let params = match params {
Some(params) => util::byond_to_params(&params)?,
None => SqlParams::Empty,
};
match RUNTIME
.read()
.block_on(query::do_query(pool, query, params))
{
Ok(result) => result.to_byond(),
Err(err) => {
let mut result = ByondValue::new_list()?;
result.write_list_index("status", "err")?;
result.write_list_index("data", err.to_string())?;
Ok(result)
}
}
}

#[dynamic(drop)]
static mut JOBS: AHashMap<usize, Job> = AHashMap::with_capacity(32);

#[byond_fn]
pub fn sql_query_async(
handle: ByondSlotKey,
query: String,
params: Option<ByondValue>,
) -> ByondResult<usize> {
let pool = match pool::get_conn(handle).expect("failed to get connection") {
Some(pool) => pool,
None => unimplemented!("pool offline"),
};
let params = match params {
Some(params) => util::byond_to_params(&params)?,
None => SqlParams::Empty,
};
let (tx, rx) = crossbeam_channel::unbounded();
let handle = thread::spawn(move || {
let result = RUNTIME
.read()
.block_on(query::do_query(pool, query, params))
.map_err(|err| err.to_string());
let _ = tx.send(result);
});
let id = NEXT_JOB_ID.fetch_add(1, Ordering::Relaxed);
JOBS.write().insert(id, Job { rx, handle });
Ok(id)
}

#[byond_fn]
pub fn sql_query_check_job(id: usize) -> ByondResult<ByondValue> {
let mut jobs = JOBS.write();
let entry = match jobs.entry(id) {
Entry::Occupied(occupied) => occupied,
Entry::Vacant(_) => return "NO SUCH JOB".to_byond(),
};
let result = match entry.get().rx.try_recv() {
Ok(result) => match result {
Ok(result) => result.to_byond()?,
Err(err) => {
let mut result = ByondValue::new_list()?;
result.write_list_index("status", "err")?;
result.write_list_index("data", err.to_string())?;
result
}
},
Err(TryRecvError::Disconnected) => return "JOB_PANICKED".to_byond(),
Err(TryRecvError::Empty) => return "NO RESULTS YET".to_byond(),
};
let _ = entry.remove().handle.join();
Ok(result)
}
35 changes: 0 additions & 35 deletions crates/sql/src/opts.rs

This file was deleted.

83 changes: 0 additions & 83 deletions crates/sql/src/pool.rs

This file was deleted.

Loading

0 comments on commit 6cc86f5

Please sign in to comment.