Try to fix the threading

Add explicit tokio::spawn in controller
Add an arbiter for the app_state in core
This commit is contained in:
Tobias Reisinger 2024-03-28 01:23:49 +01:00
parent 6d31e1e2c9
commit 1d4e9efa15
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
5 changed files with 21 additions and 10 deletions

BIN
Cargo.lock generated

Binary file not shown.

View file

@ -10,9 +10,9 @@ emgauwa-lib = { path = "../emgauwa-lib" }
actix = "0.13" actix = "0.13"
tokio = { version = "1.34", features = ["io-std", "macros", "rt-multi-thread"] } tokio = { version = "1.34", features = ["io-std", "macros", "rt-multi-thread"] }
tokio-tungstenite = "0.20" tokio-tungstenite = "0.21"
simple_logger = "4.2" simple_logger = "4.3"
log = "0.4" log = "0.4"
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }

View file

@ -112,10 +112,10 @@ async fn main() -> Result<(), std::io::Error> {
); );
tokio::select! { let _ = tokio::join!(
_ = run_relays_loop(app_state.clone()) => {}, tokio::spawn(run_relays_loop(app_state.clone())),
_ = run_ws_loop(pool.clone(), app_state.clone(), url) => {}, tokio::spawn(run_ws_loop(pool.clone(), app_state.clone(), url)),
} );
Ok(()) Ok(())
} }

View file

@ -10,7 +10,7 @@ emgauwa-lib = { path = "../emgauwa-lib" }
actix = "0.13" actix = "0.13"
actix-web = "4.4" actix-web = "4.4"
actix-web-actors = "4.2" actix-web-actors = "4.2"
actix-cors = "0.6" actix-cors = "0.7"
log = "0.4" log = "0.4"
@ -23,4 +23,5 @@ serde_derive = "1.0"
sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio", "macros", "chrono"] } sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio", "macros", "chrono"] }
futures = "0.3.29" futures = "0.3"
tokio = { version = "1.36", features = ["rt", "rt-multi-thread"] }

View file

@ -1,6 +1,6 @@
use std::net::TcpListener; use std::net::TcpListener;
use actix::Actor; use actix::{Actor, Arbiter};
use actix_cors::Cors; use actix_cors::Cors;
use actix_web::middleware::TrailingSlash; use actix_web::middleware::TrailingSlash;
use actix_web::{middleware, web, App, HttpServer}; use actix_web::{middleware, web, App, HttpServer};
@ -32,7 +32,17 @@ async fn main() -> Result<(), std::io::Error> {
.map_err(EmgauwaError::from)?; .map_err(EmgauwaError::from)?;
conn.close().await.map_err(EmgauwaError::from)?; conn.close().await.map_err(EmgauwaError::from)?;
let app_state = AppState::new(pool.clone()).start(); let app_state_arbiter = Arbiter::with_tokio_rt(|| {
tokio::runtime::Builder::new_multi_thread()
.worker_threads(2)
.enable_all()
.build()
.unwrap()
});
let app_state_pool = pool.clone();
let app_state = Actor::start_in_arbiter(&app_state_arbiter.handle(), move |_| {
AppState::new(app_state_pool.clone())
});
log::info!( log::info!(
"Starting server on {}:{}", "Starting server on {}:{}",