pub struct OrderSystem {
pub order_client: OrderClient,
pub user_client: UserClient,
pub product_client: ProductClient,
handles: Vec<JoinHandle<()>>,
}Expand description
The main runtime orchestrator for the actor-based order management system.
OrderSystem is responsible for:
- Lifecycle Management: Starting and stopping all actors in the system
- Dependency Wiring: Connecting actors that depend on each other (e.g., OrderClient needs UserClient)
- Resource Coordination: Managing shared resources like ID generators
§Architecture
The system consists of three main actors:
- User Actor: Manages user entities (CRUD operations)
- Product Actor: Manages product entities with stock tracking
- Order Actor: Manages orders and coordinates with User and Product actors
§Example
ⓘ
let system = OrderSystem::new();
// Use the clients to interact with actors
let user_id = system.user_client.create_user(user_data).await?;
let product_id = system.product_client.create_product(product_data).await?;
let order_id = system.order_client.create_order(order_data).await?;
// Gracefully shut down when done
system.shutdown().await?;Fields§
§order_client: OrderClientClient for interacting with the Order actor
user_client: UserClientClient for interacting with the User actor
product_client: ProductClientClient for interacting with the Product actor
handles: Vec<JoinHandle<()>>Task handles for all running actors (used for graceful shutdown)
Implementations§
Source§impl OrderSystem
impl OrderSystem
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates and initializes a new OrderSystem with all actors running.
This method:
- Creates ID generators for each entity type
- Spawns ResourceActors for User, Product, and Order
- Wires up dependencies (OrderClient depends on UserClient and ProductClient)
- Spawns each actor in its own Tokio task
§Returns
A fully initialized OrderSystem with all actors running and ready to accept requests.
Sourcepub async fn shutdown(self) -> Result<(), String>
pub async fn shutdown(self) -> Result<(), String>
Gracefully shuts down the entire system.
This method:
- Drops all clients, which closes their communication channels
- Waits for all actor tasks to complete
- Returns an error if any actor task panicked
§Shutdown Process
When clients are dropped, the underlying channels are closed. Each ResourceActor
detects the closed channel and exits its event loop gracefully.
§Returns
Ok(())if all actors shut down cleanlyErr(String)if any actor task failed or panicked
§Example
ⓘ
let system = OrderSystem::new();
// ... use the system ...
system.shutdown().await?;Trait Implementations§
Auto Trait Implementations§
impl Freeze for OrderSystem
impl RefUnwindSafe for OrderSystem
impl Send for OrderSystem
impl Sync for OrderSystem
impl Unpin for OrderSystem
impl UnwindSafe for OrderSystem
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more