pub struct Registry { /* private fields */ }Expand description
Implementations§
Source§impl Registry
impl Registry
Sourcepub fn new<T: Into<String>>(name: T) -> Self
pub fn new<T: Into<String>>(name: T) -> Self
Constructs a new local Registry with the given name.
Names must be unique on a per-node basis.
Examples found in repository?
58 async fn start(&self) -> Result<Pid, ExitReason> {
59 // Spawn a registry that will take care of registering 'MySpace'.
60 let children = [
61 Registry::new("space-registry")
62 .with_start(|key| {
63 let RegistryKey::String(id) = key else {
64 panic!()
65 };
66
67 MySpace::new(id).start_link(GenServerOptions::new())
68 })
69 .with_shutdown(Shutdown::Infinity)
70 .child_spec(RegistryOptions::new())
71 .id("space-registry"),
72 ChildSpec::new("test-registry")
73 .start(move || async { Ok(Process::spawn(test_registry())) }),
74 ];
75
76 // Restart only the terminated child.
77 Supervisor::with_children(children)
78 .strategy(SupervisionStrategy::OneForOne)
79 .start_link(SupervisorOptions::new())
80 .await
81 }Sourcepub fn with_start<T, F>(self, start: T) -> Selfwhere
T: Fn(RegistryKey) -> F + Send + Sync + 'static,
F: Future<Output = Result<Pid, ExitReason>> + Send + Sync + 'static,
pub fn with_start<T, F>(self, start: T) -> Selfwhere
T: Fn(RegistryKey) -> F + Send + Sync + 'static,
F: Future<Output = Result<Pid, ExitReason>> + Send + Sync + 'static,
Assigns a start routine for the registry to be able to dynamically spawn processes.
Must return a future that resolves to Result<Pid, ExitReason>.
Examples found in repository?
58 async fn start(&self) -> Result<Pid, ExitReason> {
59 // Spawn a registry that will take care of registering 'MySpace'.
60 let children = [
61 Registry::new("space-registry")
62 .with_start(|key| {
63 let RegistryKey::String(id) = key else {
64 panic!()
65 };
66
67 MySpace::new(id).start_link(GenServerOptions::new())
68 })
69 .with_shutdown(Shutdown::Infinity)
70 .child_spec(RegistryOptions::new())
71 .id("space-registry"),
72 ChildSpec::new("test-registry")
73 .start(move || async { Ok(Process::spawn(test_registry())) }),
74 ];
75
76 // Restart only the terminated child.
77 Supervisor::with_children(children)
78 .strategy(SupervisionStrategy::OneForOne)
79 .start_link(SupervisorOptions::new())
80 .await
81 }Sourcepub fn with_shutdown(self, shutdown: Shutdown) -> Self
pub fn with_shutdown(self, shutdown: Shutdown) -> Self
Sets the method used to shutdown any registered processes once when the Registry is terminated.
Examples found in repository?
58 async fn start(&self) -> Result<Pid, ExitReason> {
59 // Spawn a registry that will take care of registering 'MySpace'.
60 let children = [
61 Registry::new("space-registry")
62 .with_start(|key| {
63 let RegistryKey::String(id) = key else {
64 panic!()
65 };
66
67 MySpace::new(id).start_link(GenServerOptions::new())
68 })
69 .with_shutdown(Shutdown::Infinity)
70 .child_spec(RegistryOptions::new())
71 .id("space-registry"),
72 ChildSpec::new("test-registry")
73 .start(move || async { Ok(Process::spawn(test_registry())) }),
74 ];
75
76 // Restart only the terminated child.
77 Supervisor::with_children(children)
78 .strategy(SupervisionStrategy::OneForOne)
79 .start_link(SupervisorOptions::new())
80 .await
81 }Sourcepub async fn lookup<T: Into<Dest>, N: Into<RegistryKey>>(
registry: T,
key: N,
timeout: Option<Duration>,
) -> Result<Option<Pid>, RegistryError>
pub async fn lookup<T: Into<Dest>, N: Into<RegistryKey>>( registry: T, key: N, timeout: Option<Duration>, ) -> Result<Option<Pid>, RegistryError>
Looks up a running process.
If the registry is local, this will just query the table without hitting the registry process.
Examples found in repository?
22async fn test_registry() {
23 Registry::start_process("space-registry", "my awesome space id", None)
24 .await
25 .expect("Failed to start process");
26
27 Registry::start_process("space-registry", "my lame space id", None)
28 .await
29 .expect("Failed to start process");
30
31 let pid = Registry::lookup("space-registry", "my awesome space id", None)
32 .await
33 .expect("Failed to lookup process");
34
35 tracing::info!("Looked up space process: {:?}", pid);
36
37 let count = Registry::count("space-registry", None)
38 .await
39 .expect("Failed to count processes");
40
41 tracing::info!("Count of registered processes: {:?}", count);
42
43 let list = Registry::list("space-registry", None)
44 .await
45 .expect("Failed to list processes");
46
47 tracing::info!("List of registered processes: {:?}", list);
48}Sourcepub async fn lookup_or_start<T: Into<Dest>, N: Into<RegistryKey>>(
registry: T,
key: N,
timeout: Option<Duration>,
) -> Result<Pid, RegistryError>
pub async fn lookup_or_start<T: Into<Dest>, N: Into<RegistryKey>>( registry: T, key: N, timeout: Option<Duration>, ) -> Result<Pid, RegistryError>
Attempts to lookup a running process.
If the process isn’t currently running, it is spawned and passed the key.
Sourcepub async fn start_process<T: Into<Dest>, N: Into<RegistryKey>>(
registry: T,
key: N,
timeout: Option<Duration>,
) -> Result<Pid, RegistryError>
pub async fn start_process<T: Into<Dest>, N: Into<RegistryKey>>( registry: T, key: N, timeout: Option<Duration>, ) -> Result<Pid, RegistryError>
Attempts to start a process.
If the process is already running, an error is returned.
Examples found in repository?
22async fn test_registry() {
23 Registry::start_process("space-registry", "my awesome space id", None)
24 .await
25 .expect("Failed to start process");
26
27 Registry::start_process("space-registry", "my lame space id", None)
28 .await
29 .expect("Failed to start process");
30
31 let pid = Registry::lookup("space-registry", "my awesome space id", None)
32 .await
33 .expect("Failed to lookup process");
34
35 tracing::info!("Looked up space process: {:?}", pid);
36
37 let count = Registry::count("space-registry", None)
38 .await
39 .expect("Failed to count processes");
40
41 tracing::info!("Count of registered processes: {:?}", count);
42
43 let list = Registry::list("space-registry", None)
44 .await
45 .expect("Failed to list processes");
46
47 tracing::info!("List of registered processes: {:?}", list);
48}Sourcepub async fn stop_process<T: Into<Dest>, N: Into<RegistryKey>>(
registry: T,
key: N,
timeout: Option<Duration>,
) -> Result<(), RegistryError>
pub async fn stop_process<T: Into<Dest>, N: Into<RegistryKey>>( registry: T, key: N, timeout: Option<Duration>, ) -> Result<(), RegistryError>
Stops a process registered in the given registry with the given key.
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.
Sourcepub async fn count<T: Into<Dest>>(
registry: T,
timeout: Option<Duration>,
) -> Result<usize, RegistryError>
pub async fn count<T: Into<Dest>>( registry: T, timeout: Option<Duration>, ) -> Result<usize, RegistryError>
Returns the number of registered processes for the given registry.
Examples found in repository?
22async fn test_registry() {
23 Registry::start_process("space-registry", "my awesome space id", None)
24 .await
25 .expect("Failed to start process");
26
27 Registry::start_process("space-registry", "my lame space id", None)
28 .await
29 .expect("Failed to start process");
30
31 let pid = Registry::lookup("space-registry", "my awesome space id", None)
32 .await
33 .expect("Failed to lookup process");
34
35 tracing::info!("Looked up space process: {:?}", pid);
36
37 let count = Registry::count("space-registry", None)
38 .await
39 .expect("Failed to count processes");
40
41 tracing::info!("Count of registered processes: {:?}", count);
42
43 let list = Registry::list("space-registry", None)
44 .await
45 .expect("Failed to list processes");
46
47 tracing::info!("List of registered processes: {:?}", list);
48}Sourcepub async fn list<T: Into<Dest>>(
registry: T,
timeout: Option<Duration>,
) -> Result<Vec<(RegistryKey, Pid)>, RegistryError>
pub async fn list<T: Into<Dest>>( registry: T, timeout: Option<Duration>, ) -> Result<Vec<(RegistryKey, Pid)>, RegistryError>
Returns a list of every process registered to the given registry.
There is no ordering guarantee.
Examples found in repository?
22async fn test_registry() {
23 Registry::start_process("space-registry", "my awesome space id", None)
24 .await
25 .expect("Failed to start process");
26
27 Registry::start_process("space-registry", "my lame space id", None)
28 .await
29 .expect("Failed to start process");
30
31 let pid = Registry::lookup("space-registry", "my awesome space id", None)
32 .await
33 .expect("Failed to lookup process");
34
35 tracing::info!("Looked up space process: {:?}", pid);
36
37 let count = Registry::count("space-registry", None)
38 .await
39 .expect("Failed to count processes");
40
41 tracing::info!("Count of registered processes: {:?}", count);
42
43 let list = Registry::list("space-registry", None)
44 .await
45 .expect("Failed to list processes");
46
47 tracing::info!("List of registered processes: {:?}", list);
48}Sourcepub async fn remove<T: Into<Dest>, N: Into<RegistryKey>>(
registry: T,
key: N,
timeout: Option<Duration>,
) -> Result<Option<Pid>, RegistryError>
pub async fn remove<T: Into<Dest>, N: Into<RegistryKey>>( registry: T, key: N, timeout: Option<Duration>, ) -> Result<Option<Pid>, RegistryError>
Removes a process registered in the given registry with the given key.
The process will no longer be registered, but it will remain running if it was found.
Sourcepub async fn start(self, options: RegistryOptions) -> Result<Pid, ExitReason>
pub async fn start(self, options: RegistryOptions) -> Result<Pid, ExitReason>
Create a registry proces not linked to a supervision tree.
Sourcepub async fn start_link(
self,
options: RegistryOptions,
) -> Result<Pid, ExitReason>
pub async fn start_link( self, options: RegistryOptions, ) -> Result<Pid, ExitReason>
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).
Sourcepub fn child_spec(self, options: RegistryOptions) -> ChildSpec
pub fn child_spec(self, options: RegistryOptions) -> ChildSpec
Builds a child specification for this Registry process.
Examples found in repository?
58 async fn start(&self) -> Result<Pid, ExitReason> {
59 // Spawn a registry that will take care of registering 'MySpace'.
60 let children = [
61 Registry::new("space-registry")
62 .with_start(|key| {
63 let RegistryKey::String(id) = key else {
64 panic!()
65 };
66
67 MySpace::new(id).start_link(GenServerOptions::new())
68 })
69 .with_shutdown(Shutdown::Infinity)
70 .child_spec(RegistryOptions::new())
71 .id("space-registry"),
72 ChildSpec::new("test-registry")
73 .start(move || async { Ok(Process::spawn(test_registry())) }),
74 ];
75
76 // Restart only the terminated child.
77 Supervisor::with_children(children)
78 .strategy(SupervisionStrategy::OneForOne)
79 .start_link(SupervisorOptions::new())
80 .await
81 }Trait Implementations§
Source§impl GenServer for Registry
impl GenServer for Registry
Source§async fn init(&mut self) -> Result<(), ExitReason>
async fn init(&mut self) -> Result<(), ExitReason>
start_link or start will block until it returns.Source§async fn terminate(&mut self, _reason: ExitReason)
async fn terminate(&mut self, _reason: ExitReason)
Source§async fn handle_cast(
&mut self,
message: Self::Message,
) -> Result<(), ExitReason>
async fn handle_cast( &mut self, message: Self::Message, ) -> Result<(), ExitReason>
cast messages.Source§async fn handle_call(
&mut self,
message: Self::Message,
_from: From,
) -> Result<Option<Self::Message>, ExitReason>
async fn handle_call( &mut self, message: Self::Message, _from: From, ) -> Result<Option<Self::Message>, ExitReason>
call messages. call will block until a reply is received
(unless the call times out or nodes are disconnected). Read moreSource§async fn handle_info(
&mut self,
info: Message<Self::Message>,
) -> Result<(), ExitReason>
async fn handle_info( &mut self, info: Message<Self::Message>, ) -> Result<(), ExitReason>
Source§fn start(
self,
options: GenServerOptions,
) -> impl Future<Output = Result<Pid, ExitReason>> + Send
fn start( self, options: GenServerOptions, ) -> impl Future<Output = Result<Pid, ExitReason>> + Send
Source§fn start_link(
self,
options: GenServerOptions,
) -> impl Future<Output = Result<Pid, ExitReason>> + Send
fn start_link( self, options: GenServerOptions, ) -> impl Future<Output = Result<Pid, ExitReason>> + Send
Source§fn stop<T: Into<Dest>>(
server: T,
reason: ExitReason,
timeout: Option<Duration>,
) -> impl Future<Output = Result<(), ExitReason>>
fn stop<T: Into<Dest>>( server: T, reason: ExitReason, timeout: Option<Duration>, ) -> impl Future<Output = Result<(), ExitReason>>
reason. Read moreSource§fn cast<T: Into<Dests>>(servers: T, message: Self::Message)
fn cast<T: Into<Dests>>(servers: T, message: Self::Message)
servers without waiting for a response. Read more