From 3da0a39409fa00b80ec75e83170370e5440f81fd Mon Sep 17 00:00:00 2001 From: dtzxporter Date: Wed, 29 May 2024 12:32:24 -0400 Subject: [PATCH] Create registry options, add Registry::start to start a non-linked registry. Allow overriding the name for registry as well, use at your own risk of course. --- hydra/examples/registry.rs | 11 ++++++----- hydra/src/lib.rs | 2 ++ hydra/src/registry.rs | 29 +++++++++++++++++++++-------- hydra/src/registry_options.rs | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 13 deletions(-) create mode 100644 hydra/src/registry_options.rs diff --git a/hydra/examples/registry.rs b/hydra/examples/registry.rs index cb0eb30..1b3378e 100644 --- a/hydra/examples/registry.rs +++ b/hydra/examples/registry.rs @@ -1,7 +1,5 @@ use std::time::Duration; -use hydra::ProcessFlags; -use hydra::Shutdown; use serde::Deserialize; use serde::Serialize; @@ -12,17 +10,20 @@ use hydra::GenServer; use hydra::GenServerOptions; use hydra::Pid; use hydra::Process; +use hydra::ProcessFlags; use hydra::Registry; use hydra::RegistryKey; +use hydra::RegistryOptions; +use hydra::Shutdown; use hydra::SupervisionStrategy; use hydra::Supervisor; async fn test_registry() { - Registry::start("space-registry", "my awesome space id") + Registry::start_process("space-registry", "my awesome space id") .await .expect("Failed to start process"); - Registry::start("space-registry", "my lame space id") + Registry::start_process("space-registry", "my lame space id") .await .expect("Failed to start process"); @@ -65,7 +66,7 @@ impl Application for MyApplication { MySpace::new(id).start_link(GenServerOptions::new()) }) .with_shutdown(Shutdown::Infinity) - .child_spec(GenServerOptions::new()) + .child_spec(RegistryOptions::new()) .id("space-registry"), ChildSpec::new("test-registry") .start(move || async { Ok(Process::spawn(test_registry())) }), diff --git a/hydra/src/lib.rs b/hydra/src/lib.rs index c4bfff2..0a50309 100644 --- a/hydra/src/lib.rs +++ b/hydra/src/lib.rs @@ -35,6 +35,7 @@ mod process_registry; mod receivable; mod reference; mod registry; +mod registry_options; mod restart; mod semaphore; mod serialize; @@ -66,6 +67,7 @@ pub use process_receiver::*; pub use receivable::*; pub use reference::*; pub use registry::*; +pub use registry_options::*; pub use restart::*; pub use semaphore::*; pub use shutdown::*; diff --git a/hydra/src/registry.rs b/hydra/src/registry.rs index e1c51ce..70bc46a 100644 --- a/hydra/src/registry.rs +++ b/hydra/src/registry.rs @@ -19,13 +19,13 @@ use crate::Dest; use crate::ExitReason; use crate::From; use crate::GenServer; -use crate::GenServerOptions; use crate::Message; use crate::Node; use crate::Pid; use crate::Process; use crate::ProcessFlags; use crate::Reference; +use crate::RegistryOptions; use crate::Shutdown; use crate::SystemMessage; @@ -191,7 +191,7 @@ impl Registry { /// Attempts to start a process. /// /// If the process is already running, an error is returned. - pub async fn start, N: Into>( + pub async fn start_process, N: Into>( registry: T, key: N, ) -> Result { @@ -209,7 +209,7 @@ impl Registry { /// If the process is trapping exits, it will still run, but be unregistered from this registry. /// /// If the process is not registered an error is returned. - pub async fn stop, N: Into>( + pub async fn stop_process, N: Into>( registry: T, key: N, ) -> Result<(), RegistryError> { @@ -286,18 +286,31 @@ impl Registry { } } + /// Create a registry proces not linked to a supervision tree. + pub async fn start(self, mut options: RegistryOptions) -> Result { + if options.name.is_none() { + options = options.name(self.name.clone()); + } + + GenServer::start(self, options.into()).await + } + /// Creates a registry process as part of a supervision tree. /// /// For example, this function ensures that the registry is linked to the calling process (its supervisor). - pub async fn start_link(self, mut options: GenServerOptions) -> Result { - options = options.name(self.name.clone()); + pub async fn start_link(self, mut options: RegistryOptions) -> Result { + if options.name.is_none() { + options = options.name(self.name.clone()); + } - GenServer::start_link(self, options).await + GenServer::start_link(self, options.into()).await } /// Builds a child specification for this [Registry] process. - pub fn child_spec(self, mut options: GenServerOptions) -> ChildSpec { - options = options.name(self.name.clone()); + pub fn child_spec(self, mut options: RegistryOptions) -> ChildSpec { + if options.name.is_none() { + options = options.name(self.name.clone()); + } ChildSpec::new("Registry") .start(move || self.clone().start_link(options.clone())) diff --git a/hydra/src/registry_options.rs b/hydra/src/registry_options.rs new file mode 100644 index 0000000..85e85ce --- /dev/null +++ b/hydra/src/registry_options.rs @@ -0,0 +1,32 @@ +use crate::GenServerOptions; + +/// Options used to configure a Registry. +#[derive(Debug, Default, Clone)] +pub struct RegistryOptions { + pub(crate) name: Option, +} + +impl RegistryOptions { + /// Constructs a new instance of [RegistryOptions] with the default values. + pub const fn new() -> Self { + Self { name: None } + } + + /// Specifies a name to register the Registry under. + pub fn name>(mut self, name: T) -> Self { + self.name = Some(name.into()); + self + } +} + +impl From for GenServerOptions { + fn from(mut value: RegistryOptions) -> Self { + let mut options = GenServerOptions::new(); + + if let Some(name) = value.name.take() { + options = options.name(name); + } + + options + } +}