From 1d4e9efa1561d2b5dbdd1fb393aa53f8dd133960 Mon Sep 17 00:00:00 2001 From: Tobias Reisinger Date: Thu, 28 Mar 2024 01:23:49 +0100 Subject: [PATCH] Try to fix the threading Add explicit tokio::spawn in controller Add an arbiter for the app_state in core --- Cargo.lock | Bin 65731 -> 64009 bytes emgauwa-controller/Cargo.toml | 4 ++-- emgauwa-controller/src/main.rs | 8 ++++---- emgauwa-core/Cargo.toml | 5 +++-- emgauwa-core/src/main.rs | 14 ++++++++++++-- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c43f40a05e38d060ed3ad8850d0605a05a0e03a2..a5355d672e39814674fbde6b30af59e37581591d 100644 GIT binary patch delta 758 zcmX|8OGs5w6vf~3lYRWXQiDt^O5<_%`JahOK@Eb)2MVHK=Nn?^z(R`3gdoD1XI7IS zY*MLon>K3DhoEQ@L5ASeBGV!(qWc~co5NacID4(V_S2d2o10}z&&uMB*?dn>UI=W6 zu{wA{7^2uPXAmH5ghdJgaoTaOEmgp*0w*0y+gNZF2#M>{*<`#tTbgM@Ih<3;^~}Bh zyt8_xBdN=+HddUU7#p3KJKB}B9GfhQ(AE+cjxQH5d2nL7;osk`p+A+eZ@4v{w4dTB zyFK!*{%(SGa71!Jv_!yzuu6NbJkx=KW->@H;0SQ35Jdq8P%JHKJt*p9Uu`z->#Ry@ z-C|`?@Wa?(eUS`YO%4`G=lrkg5)$tW<-5J`lv`=j{TMhZ9Fhi0BQ*sTf}z?wkAf5y zd#@GqARIVmv^9bFPzWQwT09i@KWOy<&~|Ml(S26b-^m&WCW ze6N+nFb*!Y&nZSgkrIY;&n*~3F&2^#qI`NkiDswD*5?#prSCjNBIDnrAnzmPWL z`etJ^YuTvRTGQ7!w^b3#7MkOkjjG7Lti@{^`Pln)D#e#Y9==^H;_vTQig@eCrDSDm GHS-tZ*8(B{ delta 1536 zcmZ`(O=wRe(`H63UBe=+ zb#?fj=FE=a!REb@PX-%1e&wOjSn_s^+9sdUmRQcVw*oFfiFP-ya!nK^HtXog$M+R%I&IN|!Ww z0hEeZviEZU} z6qqm>FO|_+MFT|!Vp~(nnV;54JklzV;=%S<>gL8H_1TRzMw|V%dq`kLv_O_SWOEU% zfgq3osUQUmycu-J#yLwSCbl!k>_KJT2#}yoPCvi`jn&EPzx3!Wur-objyVy?MBWn0 zB9sU8+URW2Y40@qD&$C@C76g*a`bFlLMZiu+RKpl)}OEc*n>O-kPx_7b{_ZwC>S|% z)x4jgHSP3zl^7mON*;uAq3}ph1pZNqdh*gV175og)_o#nmWm6*lG$`oSU~o35#UlH z;ay=YBXgl-cv;BA?!;n42u{RMH!rtXWA)&TiF$hTryjw1WfRB%mNdT@?Es0jFOs2o z2|41$5)^A@i46}Hv`5GsHInx&P+!G^H-6nxu^ltsMp{dwK(g32Ndk(3f;7BPz(*0h d5vC+A6_n-{7sLIN!UkSxTPlt Result<(), std::io::Error> { ); - tokio::select! { - _ = run_relays_loop(app_state.clone()) => {}, - _ = run_ws_loop(pool.clone(), app_state.clone(), url) => {}, - } + let _ = tokio::join!( + tokio::spawn(run_relays_loop(app_state.clone())), + tokio::spawn(run_ws_loop(pool.clone(), app_state.clone(), url)), + ); Ok(()) } diff --git a/emgauwa-core/Cargo.toml b/emgauwa-core/Cargo.toml index db61748..6400d42 100644 --- a/emgauwa-core/Cargo.toml +++ b/emgauwa-core/Cargo.toml @@ -10,7 +10,7 @@ emgauwa-lib = { path = "../emgauwa-lib" } actix = "0.13" actix-web = "4.4" actix-web-actors = "4.2" -actix-cors = "0.6" +actix-cors = "0.7" log = "0.4" @@ -23,4 +23,5 @@ serde_derive = "1.0" 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"] } diff --git a/emgauwa-core/src/main.rs b/emgauwa-core/src/main.rs index ba632af..1f3e1dd 100644 --- a/emgauwa-core/src/main.rs +++ b/emgauwa-core/src/main.rs @@ -1,6 +1,6 @@ use std::net::TcpListener; -use actix::Actor; +use actix::{Actor, Arbiter}; use actix_cors::Cors; use actix_web::middleware::TrailingSlash; use actix_web::{middleware, web, App, HttpServer}; @@ -32,7 +32,17 @@ async fn main() -> Result<(), std::io::Error> { .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!( "Starting server on {}:{}",