Expand description
Process groups (PG) are named groups of actors with a friendly name which can be used for retrieval of the process groups. Then within the group, either a random actor (for dispatch) can be selected or the whole group (broadcast), or a subset (partial-broadcast) can have a message sent to them. Common operations are to (a) upcast the group members to a strong-type’d actor then dispatch a message with crate::call or crate::cast.
Process groups can also be monitored for changes with calling monitor to subscribe to changes and demonitor to unsubscribe. Subscribers will receive process group change notifications via a SupervisionEvent called on the supervision port of the crate::Actor
Inspired from Erlang’s pg
module
§Examples
use ractor::pg;
use ractor::{Actor, ActorProcessingErr, ActorRef};
struct ExampleActor;
#[cfg_attr(feature = "async-trait", ractor::async_trait)]
impl Actor for ExampleActor {
type Msg = ();
type State = ();
type Arguments = ();
async fn pre_start(
&self,
_myself: ActorRef<Self::Msg>,
_args: Self::Arguments,
) -> Result<Self::State, ActorProcessingErr> {
println!("Starting");
Ok(())
}
}
#[tokio::main]
async fn main() {
let (actor, handle) = Actor::spawn(None, ExampleActor, ())
.await
.expect("Failed to startup dummy actor");
let group = "the_group".to_string();
// Join the actor to a group. This is also commonly done in `pre_start` or `post_start`
// of the actor itself without having to do it externally by some coordinator
pg::join(group.clone(), vec![actor.get_cell()]);
// Retrieve the pg group membership
let members = pg::get_members(&group);
// Send a message to the up-casted actor
let the_actor: ActorRef<()> = members.get(0).unwrap().clone().into();
ractor::cast!(the_actor, ()).expect("Failed to send message");
// wait for actor exit
actor.stop(None);
handle.await.unwrap();
// The actor will automatically be removed from the group upon shutdown.
let members = pg::get_members(&group);
assert_eq!(members.len(), 0);
}
Structs§
- Represents the combination of a
ScopeName
and aGroupName
that uniquely identifies a specific group in a specific scope
Enums§
- Represents a change in a process group’s membership
Constants§
- Key to monitor all of the groups in a scope
- Key to monitor all of the scopes
- Key to set the default scope
Functions§
- Unsubscribes the provided crate::Actor for updates from the group in default scope
- Unsubscribes the provided crate::Actor from the scope for updates
- Returns all actors running on the local node in the group
group
in the default scope. - Returns all the actors running on any node in the group
group
in the default scope. - Returns all actors running on the local node in the group
group
in scopescope
- Returns all the actors running on any node in the group
group
in the scopescope
. - Join actors to the group
group
in the default scope - Join actors to the group
group
within the scopescope
- Leaves the specified crate::Actors from the PG group in the default scope
- Leaves the specified crate::Actors from the PG group within the scope
scope
- Subscribes the provided crate::Actor to the group in the default scope for updates
- Subscribes the provided crate::Actor to the scope for updates
- Return a list of all known groups
- Returns a list of all known groups in scope
scope
- Returns a list of all known scopes
- Returns a list of all known scope-group combinations.