Slightly reduce the amount of cloning
Some checks are pending
build / binary (push) Waiting to run
build / binary-static (push) Waiting to run
build / container (push) Waiting to run
build / clippy (push) Waiting to run
lint / linting (push) Waiting to run

Rather than cloning `Iocaine` (along with the markov chain & everything)
all over the place, declare it shared state behind an `Arc<Mutex>`. This
should reduce the amount of cloning done noticably.

Signed-off-by: Gergely Nagy <me@gergo.csillger.hu>
This commit is contained in:
Gergely Nagy 2025-02-03 23:37:03 +01:00
parent ff9c31d326
commit a0410a528d
No known key found for this signature in database
3 changed files with 19 additions and 11 deletions

View file

@ -5,18 +5,21 @@
use anyhow::Result;
use axum::{
extract::{Extension, Path},
extract::{Path, State},
response::Html,
routing::get,
Router,
};
use std::sync::{Arc, Mutex};
use crate::{
assembled_statistical_sequences::AssembledStatisticalSequences, config::Config,
garglebargle::GargleBargle, wurstsalat_generator_pro::WurstsalatGeneratorPro,
};
#[derive(Debug, Clone)]
type StatefulIocaine = Arc<Mutex<Iocaine>>;
#[derive(Debug)]
pub struct Iocaine {
pub config: Config,
pub chain: WurstsalatGeneratorPro,
@ -41,13 +44,14 @@ impl Iocaine {
})
}
pub async fn run(&self) -> std::result::Result<(), std::io::Error> {
pub async fn run(self) -> std::result::Result<(), std::io::Error> {
let bind = &self.config.server.bind.clone();
let state: StatefulIocaine = Arc::new(Mutex::new(self));
let app = Router::new()
.route("/", get(poison_root))
.route("/{*path}", get(poison_path))
.layer(Extension(self.clone()))
.layer(tower_http::trace::TraceLayer::new_for_http());
.layer(tower_http::trace::TraceLayer::new_for_http())
.with_state(state);
let listener = tokio::net::TcpListener::bind(bind).await?;
axum::serve(listener, app)
.with_graceful_shutdown(shutdown_signal())
@ -75,7 +79,7 @@ async fn shutdown_signal() {
}
}
fn poison(iocaine: &Iocaine, headers: axum::http::HeaderMap, path: &str) -> Html<String> {
fn poison(iocaine: &StatefulIocaine, headers: axum::http::HeaderMap, path: &str) -> Html<String> {
let default_host = axum::http::HeaderValue::from_static("<unknown>");
let host = headers
.get("host")
@ -83,19 +87,23 @@ fn poison(iocaine: &Iocaine, headers: axum::http::HeaderMap, path: &str) -> Html
.to_str()
.unwrap();
Html(AssembledStatisticalSequences::generate(iocaine, host, path))
Html(AssembledStatisticalSequences::generate(
&iocaine.lock().unwrap(),
host,
path,
))
}
async fn poison_root(
headers: axum::http::HeaderMap,
Extension(iocaine): Extension<Iocaine>,
State(iocaine): State<StatefulIocaine>,
) -> Html<String> {
poison(&iocaine, headers, "/")
}
async fn poison_path(
headers: axum::http::HeaderMap,
Extension(iocaine): Extension<Iocaine>,
State(iocaine): State<StatefulIocaine>,
Path(path): Path<String>,
) -> Html<String> {
poison(&iocaine, headers, &path)

View file

@ -6,7 +6,7 @@
use std::fs::File;
use std::io::{self, BufRead, BufReader};
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct GargleBargle(pub Vec<String>);
impl GargleBargle {

View file

@ -13,7 +13,7 @@ use std::fs::File;
use std::io;
pub type Bigram = (String, String);
#[derive(Debug, Clone, Default)]
#[derive(Debug, Default)]
pub struct WurstsalatGeneratorPro {
map: HashMap<Bigram, Vec<String>>,
keys: Vec<Bigram>,