Skip to content

Commit

Permalink
Create registry options, add Registry::start to start a non-linked re…
Browse files Browse the repository at this point in the history
…gistry.

Allow overriding the name for registry as well, use at your own risk of course.
  • Loading branch information
dtzxporter committed May 29, 2024
1 parent 2c90ee5 commit 3da0a39
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
11 changes: 6 additions & 5 deletions hydra/examples/registry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::time::Duration;

use hydra::ProcessFlags;
use hydra::Shutdown;
use serde::Deserialize;
use serde::Serialize;

Expand All @@ -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");

Expand Down Expand Up @@ -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())) }),
Expand Down
2 changes: 2 additions & 0 deletions hydra/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ mod process_registry;
mod receivable;
mod reference;
mod registry;
mod registry_options;
mod restart;
mod semaphore;
mod serialize;
Expand Down Expand Up @@ -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::*;
Expand Down
29 changes: 21 additions & 8 deletions hydra/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<T: Into<Dest>, N: Into<RegistryKey>>(
pub async fn start_process<T: Into<Dest>, N: Into<RegistryKey>>(
registry: T,
key: N,
) -> Result<Pid, RegistryError> {
Expand All @@ -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<T: Into<Dest>, N: Into<RegistryKey>>(
pub async fn stop_process<T: Into<Dest>, N: Into<RegistryKey>>(
registry: T,
key: N,
) -> Result<(), RegistryError> {
Expand Down Expand Up @@ -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<Pid, ExitReason> {
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<Pid, ExitReason> {
options = options.name(self.name.clone());
pub async fn start_link(self, mut options: RegistryOptions) -> Result<Pid, ExitReason> {
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()))
Expand Down
32 changes: 32 additions & 0 deletions hydra/src/registry_options.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::GenServerOptions;

/// Options used to configure a Registry.
#[derive(Debug, Default, Clone)]
pub struct RegistryOptions {
pub(crate) name: Option<String>,
}

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<T: Into<String>>(mut self, name: T) -> Self {
self.name = Some(name.into());
self
}
}

impl From<RegistryOptions> 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
}
}

0 comments on commit 3da0a39

Please sign in to comment.