From bc771e21cae3c0416c9fb20fa027e1a008e44ace Mon Sep 17 00:00:00 2001 From: Giacomo Pasini Date: Thu, 27 Oct 2022 12:09:40 +0200 Subject: [PATCH] Remove multiple runtimes support from API (#5) --- overwatch-derive/src/lib.rs | 12 +++------ overwatch/src/overwatch/mod.rs | 20 +++------------ overwatch/src/services/handle.rs | 42 +++++++++----------------------- overwatch/src/services/mod.rs | 12 --------- 4 files changed, 19 insertions(+), 67 deletions(-) diff --git a/overwatch-derive/src/lib.rs b/overwatch-derive/src/lib.rs index 69335e4..a5894a6 100644 --- a/overwatch-derive/src/lib.rs +++ b/overwatch-derive/src/lib.rs @@ -142,32 +142,26 @@ fn generate_new_impl(fields: &Punctuated) -> proc_macro2::TokenStr let settings_field_identifier = service_settings_field_identifier_from(field_identifier); quote! { #field_identifier: { - let (runtime, manager) = + let manager = ::overwatch::services::handle::ServiceHandle::<#service_type>::new( #settings_field_identifier, overwatch_handle.clone(), ); - runtimes.insert(<#service_type as ::overwatch::services::ServiceData>::SERVICE_ID, runtime); manager } } }); quote! { - fn new(settings: Self::Settings, overwatch_handle: ::overwatch::overwatch::handle::OverwatchHandle) -> ( - std::collections::HashMap<::overwatch::services::ServiceId, ::std::option::Option<::tokio::runtime::Runtime>>, - Self - ) { + fn new(settings: Self::Settings, overwatch_handle: ::overwatch::overwatch::handle::OverwatchHandle) -> Self { let Self::Settings { #( #fields_settings ),* } = settings; - let mut runtimes = ::std::collections::HashMap::new(); - let app = Self { #( #managers ),* }; - (runtimes, app) + app } } } diff --git a/overwatch/src/overwatch/mod.rs b/overwatch/src/overwatch/mod.rs index 9737f06..c416d0f 100644 --- a/overwatch/src/overwatch/mod.rs +++ b/overwatch/src/overwatch/mod.rs @@ -3,7 +3,6 @@ pub mod handle; // std use std::any::Any; -use std::collections::HashMap; use std::fmt::Debug; use std::future::Future; @@ -55,10 +54,7 @@ pub trait Services: Send + Sync { /// It returns a `(ServiceId, Runtime)` where Runtime is the `tokio::runtime::Runtime` attached for each /// service. /// It also returns an instance of the implementing type. - fn new( - settings: Self::Settings, - overwatch_handle: OverwatchHandle, - ) -> (HashMap>, Self); + fn new(settings: Self::Settings, overwatch_handle: OverwatchHandle) -> Self; /// Start a services attached to the trait implementer fn start(&mut self, service_id: ServiceId) -> Result<(), Error>; @@ -106,7 +102,7 @@ where let (finish_signal_sender, finish_runner_signal) = tokio::sync::oneshot::channel(); let (commands_sender, commands_receiver) = tokio::sync::mpsc::channel(16); let handle = OverwatchHandle::new(runtime.handle().clone(), commands_sender); - let (services_runtimes, services) = S::new(settings, handle.clone()); + let services = S::new(settings, handle.clone()); let runner = OverwatchRunner { services, handle: handle.clone(), @@ -115,7 +111,6 @@ where runtime.spawn(async move { runner.run_(commands_receiver).await }); Overwatch { runtime, - services_runtimes, handle, finish_runner_signal, } @@ -189,8 +184,6 @@ where /// It manages the overwatch runtime and handle pub struct Overwatch { runtime: Runtime, - #[allow(unused)] - services_runtimes: HashMap>, handle: OverwatchHandle, finish_runner_signal: oneshot::Receiver, } @@ -236,9 +229,7 @@ mod test { use crate::overwatch::{Error, OverwatchRunner, Services}; use crate::services::relay::{RelayError, RelayResult}; use crate::services::ServiceId; - use std::collections::HashMap; use std::time::Duration; - use tokio::runtime::Runtime; use tokio::time::sleep; struct EmptyServices; @@ -246,11 +237,8 @@ mod test { impl Services for EmptyServices { type Settings = (); - fn new( - _settings: Self::Settings, - _overwatch_handle: OverwatchHandle, - ) -> (HashMap>, Self) { - (HashMap::new(), EmptyServices) + fn new(_settings: Self::Settings, _overwatch_handle: OverwatchHandle) -> Self { + EmptyServices } fn start(&mut self, service_id: ServiceId) -> Result<(), Error> { diff --git a/overwatch/src/services/handle.rs b/overwatch/src/services/handle.rs index aca71f3..1874ef9 100644 --- a/overwatch/src/services/handle.rs +++ b/overwatch/src/services/handle.rs @@ -2,7 +2,7 @@ use std::marker::PhantomData; // crates use futures::future::{abortable, AbortHandle}; -use tokio::runtime::{Handle, Runtime}; +use tokio::runtime::Handle; use tracing::instrument; // internal use crate::overwatch::handle::OverwatchHandle; @@ -19,8 +19,6 @@ use crate::services::{ServiceCore, ServiceId, ServiceState}; pub struct ServiceHandle { /// Service id (must match ``) id: ServiceId, - /// Service runtime handle - runtime: Handle, /// Message channel relay /// Would be None if service is not running /// Will contain the channel if service is running @@ -35,8 +33,6 @@ pub struct ServiceHandle { /// Service core resources /// It contains whatever is necessary to start a new service runner pub struct ServiceStateHandle { - /// Service runtime handler - pub runtime: Handle, /// Relay channel to communicate with the service runner pub inbound_relay: InboundRelay, /// Overwatch handle @@ -49,37 +45,25 @@ pub struct ServiceStateHandle { /// Main service executor /// It is the object that hold the necessary information for the service to run pub struct ServiceRunner { - #[allow(unused)] - overwatch_handle: OverwatchHandle, service_state: ServiceStateHandle, state_handle: StateHandle, } impl ServiceHandle { - pub fn new( - settings: S::Settings, - overwatch_handle: OverwatchHandle, - ) -> (Option, Self) { + pub fn new(settings: S::Settings, overwatch_handle: OverwatchHandle) -> Self { let id = S::SERVICE_ID; - let runtime = S::service_runtime(&settings, overwatch_handle.runtime()); - let initial_state: S::State = S::State::from_settings(&settings); - let handle = runtime.handle(); let settings = SettingsUpdater::new(settings); - ( - runtime.runtime(), - Self { - id, - runtime: handle, - outbound_relay: None, - settings, - initial_state, - overwatch_handle, - _marker: PhantomData::default(), - }, - ) + Self { + id, + outbound_relay: None, + settings, + initial_state, + overwatch_handle, + _marker: PhantomData::default(), + } } pub fn id(&self) -> ServiceId { @@ -89,7 +73,7 @@ impl ServiceHandle { /// Service runtime getter /// it is easily cloneable and can be done on demand pub fn runtime(&self) -> &Handle { - &self.runtime + self.overwatch_handle.runtime() } /// Overwatch handle @@ -121,7 +105,6 @@ impl ServiceHandle { StateHandle::::new(self.initial_state.clone(), operator); let service_state = ServiceStateHandle { - runtime: self.runtime.clone(), inbound_relay, overwatch_handle: self.overwatch_handle.clone(), state_updater, @@ -130,7 +113,6 @@ impl ServiceHandle { }; ServiceRunner { - overwatch_handle: self.overwatch_handle().clone(), service_state, state_handle, } @@ -154,7 +136,7 @@ impl ServiceRunner { .. } = self; - let runtime = service_state.runtime.clone(); + let runtime = service_state.overwatch_handle.runtime().clone(); let service = S::init(service_state); let (runner, abortable_handle) = abortable(service.run()); diff --git a/overwatch/src/services/mod.rs b/overwatch/src/services/mod.rs index b69b4a2..5c8e5c0 100644 --- a/overwatch/src/services/mod.rs +++ b/overwatch/src/services/mod.rs @@ -45,18 +45,6 @@ pub trait ServiceCore: ServiceData + Send + Sized + 'static { /// Initialize the service with the given state fn init(service_state: ServiceStateHandle) -> Self; - /// Service default runtime - fn service_runtime(_settings: &Self::Settings, _parent: &runtime::Handle) -> ServiceRuntime { - let id = Self::SERVICE_ID; - ServiceRuntime::Custom( - runtime::Builder::new_multi_thread() - .enable_all() - .thread_name(id) - .build() - .unwrap_or_else(|_| panic!("Async runtime for service {id} didn't build properly")), - ) - } - /// Service main loop async fn run(mut self); }