Skip to content

Commit

Permalink
Make App and Server share the same one-threaded async runtime
Browse files Browse the repository at this point in the history
saves one thread
  • Loading branch information
helgoboss committed Nov 16, 2023
1 parent 4598677 commit 8cea618
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
22 changes: 17 additions & 5 deletions main/src/infrastructure/plugin/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use crate::infrastructure::plugin::debug_util::resolve_symbols_from_clipboard;
use crate::infrastructure::plugin::tracing_util::TracingHook;
use crate::infrastructure::server::services::RealearnServices;
use crate::infrastructure::test::run_test;
use anyhow::bail;
use base::metrics_util::MetricsHook;
use helgoboss_allocator::{start_async_deallocation_thread, AsyncDeallocatorCommandReceiver};
use once_cell::sync::Lazy;
Expand Down Expand Up @@ -450,7 +451,7 @@ impl App {
if self.config.borrow().server_is_enabled() {
self.server()
.borrow_mut()
.start(self.create_services())
.start(&async_runtime, self.create_services())
.unwrap_or_else(warn_about_failed_server_start);
}
let mut session = Reaper::get().medium_session();
Expand Down Expand Up @@ -744,11 +745,16 @@ impl App {
where
R: Send + 'static,
{
self.with_async_runtime(|runtime| runtime.spawn(f))
.expect("couldn't use runtime")
}

fn with_async_runtime<R>(&self, f: impl FnOnce(&Runtime) -> R) -> anyhow::Result<R> {
let state = self.state.borrow();
let AppState::Awake(state) = &*state else {
panic!("attempted to spawn future while ReaLearn in wrong state: {state:?}");
bail!("attempt to access async runtime while ReaLearn in wrong state: {state:?}");
};
state.async_runtime.spawn(f)
Ok(f(&state.async_runtime))
}

// TODO-medium Return a reference to a SharedControllerManager! Clients might just want to turn
Expand Down Expand Up @@ -804,9 +810,15 @@ impl App {
}

pub fn start_server_persistently(&self) -> Result<(), String> {
self.server.borrow_mut().start(self.create_services())?;
let start_result = self
.with_async_runtime(|runtime| {
self.server
.borrow_mut()
.start(runtime, self.create_services())
})
.map_err(|e| e.to_string())?;
self.change_config(AppConfig::enable_server);
Ok(())
start_result
}

pub fn stop_server_persistently(&self) {
Expand Down
46 changes: 19 additions & 27 deletions main/src/infrastructure/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::infrastructure::server::services::RealearnServices;
use derivative::Derivative;
use std::thread::JoinHandle;
use std::time::Duration;
use tokio::runtime::Runtime;

pub type SharedRealearnServer = Rc<RefCell<RealearnServer>>;

Expand Down Expand Up @@ -99,7 +100,7 @@ enum ServerState {
struct ServerRuntimeData {
clients: ServerClients,
shutdown_sender: broadcast::Sender<()>,
server_thread_join_handle: JoinHandle<()>,
server_join_handle: tokio::task::JoinHandle<()>,
}

impl ServerState {
Expand Down Expand Up @@ -135,7 +136,7 @@ impl RealearnServer {
}

/// Idempotent
pub fn start(&mut self, services: RealearnServices) -> Result<(), String> {
pub fn start(&mut self, runtime: &Runtime, services: RealearnServices) -> Result<(), String> {
if self.state.is_starting_or_running() {
return Ok(());
}
Expand All @@ -150,30 +151,20 @@ impl RealearnServer {
let key_and_cert = self.key_and_cert();
let (shutdown_sender, shutdown_receiver) = broadcast::channel(5);
let metrics_reporter = self.metrics_reporter.clone();
let server_thread_join_handle = std::thread::Builder::new()
.name("ReaLearn server".to_string())
.spawn(move || {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
runtime.block_on(start_servers(
http_port,
https_port,
grpc_port,
clients_clone,
key_and_cert,
shutdown_receiver,
metrics_reporter,
services,
));
runtime.shutdown_timeout(Duration::from_secs(1));
})
.map_err(|_| "couldn't start server thread".to_string())?;
let server_join_handle = runtime.spawn(start_servers(
http_port,
https_port,
grpc_port,
clients_clone,
key_and_cert,
shutdown_receiver,
metrics_reporter,
services,
));
let runtime_data = ServerRuntimeData {
clients,
shutdown_sender,
server_thread_join_handle,
server_join_handle,
};
self.state = ServerState::Starting(runtime_data);
self.notify_changed();
Expand Down Expand Up @@ -208,10 +199,11 @@ impl RealearnServer {
ServerState::Stopped => return,
};
let _ = runtime_data.shutdown_sender.send(());
runtime_data
.server_thread_join_handle
.join()
.expect("couldn't wait for server thread to finish");
// TODO-high-ms2 Maybe this is enough? No shutdown sender necessary?
// runtime_data
// .server_join_handle
// .join()
// .expect("couldn't wait for server thread to finish");
}

fn notify_changed(&mut self) {
Expand Down

0 comments on commit 8cea618

Please sign in to comment.