Skip to content

Commit 58a8fe9

Browse files
committed
feat(actors): add message bus actor
1 parent 96d06c8 commit 58a8fe9

File tree

5 files changed

+375
-49
lines changed

5 files changed

+375
-49
lines changed

actors/src/broker.rs

+5-48
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,12 @@
1111
//! - **Multiple Delivery Strategies**: Configure how messages are delivered to handle different reliability needs.
1212
//! - **Automatic Cleanup**: Dead actor references are automatically removed from subscription lists.
1313
//!
14-
//! # When to use Broker vs PubSub
15-
//!
16-
//! The `broker` module provides a more sophisticated routing system compared to the simpler `pubsub` module:
17-
//!
18-
//! - Use **Broker** when you need hierarchical topics, pattern-based subscriptions, or different delivery strategies.
19-
//! - Use **PubSub** when you only need simple broadcast to all listeners with optional type-based filtering.
20-
//!
2114
//! # Example
2215
//!
2316
//! ```
2417
//! use kameo::Actor;
25-
//! use kameo_actors::broker::{Broker, Subscribe, Publish, DeliveryStrategy};
18+
//! use kameo_actors::broker::{Broker, Subscribe, Publish};
19+
//! use kameo_actors::DeliveryStrategy;
2620
//! use glob::Pattern;
2721
//! # use std::time::Duration;
2822
//! # use kameo::message::{Context, Message};
@@ -62,11 +56,13 @@
6256
//! # });
6357
//! ```
6458
65-
use std::{collections::HashMap, time::Duration};
59+
use std::collections::HashMap;
6660

6761
use glob::{MatchOptions, Pattern};
6862
use kameo::prelude::*;
6963

64+
use crate::DeliveryStrategy;
65+
7066
/// A generic topic-based message broker for the actor system.
7167
///
7268
/// The broker manages subscriptions to topics and delivers messages published
@@ -268,42 +264,3 @@ impl<M: Clone + Send + 'static> Message<Publish<M>> for Broker<M> {
268264
}
269265
}
270266
}
271-
272-
/// Strategies for delivering messages to subscribers.
273-
///
274-
/// Different strategies provide different trade-offs between reliability,
275-
/// performance, and resource usage.
276-
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
277-
pub enum DeliveryStrategy {
278-
/// Block until all messages are delivered.
279-
///
280-
/// This strategy ensures reliable delivery but may cause the broker
281-
/// to block if any recipient's mailbox is full.
282-
Guaranteed,
283-
284-
/// Skip actors with full mailboxes.
285-
///
286-
/// This strategy attempts to deliver messages immediately without blocking,
287-
/// but will skip recipients whose mailboxes are full.
288-
#[default]
289-
BestEffort,
290-
291-
/// Try to deliver with timeout (blocks the publisher).
292-
///
293-
/// This strategy waits for each recipient to accept the message, but only
294-
/// up to the specified timeout duration. The broker will block during delivery.
295-
TimedDelivery(Duration),
296-
297-
/// Spawn a task for each delivery (non-blocking).
298-
///
299-
/// This strategy spawns a separate task for each message delivery,
300-
/// allowing the broker to continue processing other messages immediately.
301-
/// Tasks will retry indefinitely if mailboxes are full.
302-
Spawned,
303-
304-
/// Spawn a task with timeout for each delivery.
305-
///
306-
/// This strategy combines the benefits of spawned delivery with a timeout,
307-
/// ensuring that delivery attempts don't consume resources indefinitely.
308-
SpawnedWithTimeout(Duration),
309-
}

actors/src/lib.rs

+49
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,58 @@
33
//! This crate provides reusable actor components:
44
//!
55
//! - `broker`: Topic-based message broker
6+
//! - `message_bus`: Type-based message bus
67
//! - `pool`: Actor pool for managing concurrent task execution
78
//! - `pubsub`: Publish-subscribe pattern implementation for actor communication
9+
//!
10+
//! # When to use MessageBus vs Broker vs PubSub
11+
//!
12+
//! - Use **MessageBus** when you want to route messages based on their type without explicit topics.
13+
//! - Use **Broker** when you need hierarchical topics, pattern-based subscriptions, or explicit routing.
14+
//! - Use **PubSub** when you need simple broadcast to all listeners with optional predicate-based filtering.
15+
16+
use std::time::Duration;
817

918
pub mod broker;
19+
pub mod message_bus;
1020
pub mod pool;
1121
pub mod pubsub;
22+
23+
/// Strategies for delivering messages to subscribers.
24+
///
25+
/// Different strategies provide different trade-offs between reliability,
26+
/// performance, and resource usage.
27+
#[derive(Clone, Copy, Debug, Default, Hash, PartialEq, Eq)]
28+
pub enum DeliveryStrategy {
29+
/// Block until all messages are delivered.
30+
///
31+
/// This strategy ensures reliable delivery but may cause the broker
32+
/// to block if any recipient's mailbox is full.
33+
Guaranteed,
34+
35+
/// Skip actors with full mailboxes.
36+
///
37+
/// This strategy attempts to deliver messages immediately without blocking,
38+
/// but will skip recipients whose mailboxes are full.
39+
#[default]
40+
BestEffort,
41+
42+
/// Try to deliver with timeout (blocks the publisher).
43+
///
44+
/// This strategy waits for each recipient to accept the message, but only
45+
/// up to the specified timeout duration. The broker will block during delivery.
46+
TimedDelivery(Duration),
47+
48+
/// Spawn a task for each delivery (non-blocking).
49+
///
50+
/// This strategy spawns a separate task for each message delivery,
51+
/// allowing the broker to continue processing other messages immediately.
52+
/// Tasks will retry indefinitely if mailboxes are full.
53+
Spawned,
54+
55+
/// Spawn a task with timeout for each delivery.
56+
///
57+
/// This strategy combines the benefits of spawned delivery with a timeout,
58+
/// ensuring that delivery attempts don't consume resources indefinitely.
59+
SpawnedWithTimeout(Duration),
60+
}

0 commit comments

Comments
 (0)