mirror of
https://git.madhouse-project.org/algernon/iocaine.git
synced 2025-02-24 02:18:47 +01:00
Slightly reduce the amount of cloning
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:
parent
ff9c31d326
commit
a0410a528d
3 changed files with 19 additions and 11 deletions
26
src/app.rs
26
src/app.rs
|
@ -5,18 +5,21 @@
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::{Extension, Path},
|
extract::{Path, State},
|
||||||
response::Html,
|
response::Html,
|
||||||
routing::get,
|
routing::get,
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
assembled_statistical_sequences::AssembledStatisticalSequences, config::Config,
|
assembled_statistical_sequences::AssembledStatisticalSequences, config::Config,
|
||||||
garglebargle::GargleBargle, wurstsalat_generator_pro::WurstsalatGeneratorPro,
|
garglebargle::GargleBargle, wurstsalat_generator_pro::WurstsalatGeneratorPro,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
type StatefulIocaine = Arc<Mutex<Iocaine>>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Iocaine {
|
pub struct Iocaine {
|
||||||
pub config: Config,
|
pub config: Config,
|
||||||
pub chain: WurstsalatGeneratorPro,
|
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 bind = &self.config.server.bind.clone();
|
||||||
|
let state: StatefulIocaine = Arc::new(Mutex::new(self));
|
||||||
let app = Router::new()
|
let app = Router::new()
|
||||||
.route("/", get(poison_root))
|
.route("/", get(poison_root))
|
||||||
.route("/{*path}", get(poison_path))
|
.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?;
|
let listener = tokio::net::TcpListener::bind(bind).await?;
|
||||||
axum::serve(listener, app)
|
axum::serve(listener, app)
|
||||||
.with_graceful_shutdown(shutdown_signal())
|
.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 default_host = axum::http::HeaderValue::from_static("<unknown>");
|
||||||
let host = headers
|
let host = headers
|
||||||
.get("host")
|
.get("host")
|
||||||
|
@ -83,19 +87,23 @@ fn poison(iocaine: &Iocaine, headers: axum::http::HeaderMap, path: &str) -> Html
|
||||||
.to_str()
|
.to_str()
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
Html(AssembledStatisticalSequences::generate(iocaine, host, path))
|
Html(AssembledStatisticalSequences::generate(
|
||||||
|
&iocaine.lock().unwrap(),
|
||||||
|
host,
|
||||||
|
path,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn poison_root(
|
async fn poison_root(
|
||||||
headers: axum::http::HeaderMap,
|
headers: axum::http::HeaderMap,
|
||||||
Extension(iocaine): Extension<Iocaine>,
|
State(iocaine): State<StatefulIocaine>,
|
||||||
) -> Html<String> {
|
) -> Html<String> {
|
||||||
poison(&iocaine, headers, "/")
|
poison(&iocaine, headers, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn poison_path(
|
async fn poison_path(
|
||||||
headers: axum::http::HeaderMap,
|
headers: axum::http::HeaderMap,
|
||||||
Extension(iocaine): Extension<Iocaine>,
|
State(iocaine): State<StatefulIocaine>,
|
||||||
Path(path): Path<String>,
|
Path(path): Path<String>,
|
||||||
) -> Html<String> {
|
) -> Html<String> {
|
||||||
poison(&iocaine, headers, &path)
|
poison(&iocaine, headers, &path)
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, BufRead, BufReader};
|
use std::io::{self, BufRead, BufReader};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug)]
|
||||||
pub struct GargleBargle(pub Vec<String>);
|
pub struct GargleBargle(pub Vec<String>);
|
||||||
|
|
||||||
impl GargleBargle {
|
impl GargleBargle {
|
||||||
|
|
|
@ -13,7 +13,7 @@ use std::fs::File;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
pub type Bigram = (String, String);
|
pub type Bigram = (String, String);
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct WurstsalatGeneratorPro {
|
pub struct WurstsalatGeneratorPro {
|
||||||
map: HashMap<Bigram, Vec<String>>,
|
map: HashMap<Bigram, Vec<String>>,
|
||||||
keys: Vec<Bigram>,
|
keys: Vec<Bigram>,
|
||||||
|
|
Loading…
Reference in a new issue