use crate::dispatcher::DispatcherConfig;
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct EventBusConfig {
pub dispatcher: DispatcherConfig,
pub max_retries: u32,
pub retry_backoff: Duration,
pub shutdown_timeout: Duration,
pub enable_tracing: bool,
pub handler_channel_size: usize,
pub dlq_channel_size: usize,
pub wait_for_persistence: bool,
}
impl Default for EventBusConfig {
fn default() -> Self {
Self {
dispatcher: DispatcherConfig::default(),
max_retries: 3,
retry_backoff: Duration::from_millis(100),
shutdown_timeout: Duration::from_secs(30),
enable_tracing: true,
handler_channel_size: 256,
dlq_channel_size: 1000,
wait_for_persistence: false,
}
}
}
impl EventBusConfig {
pub fn new() -> Self {
Self::default()
}
pub fn max_retries(mut self, retries: u32) -> Self {
self.max_retries = retries;
self
}
pub fn retry_backoff(mut self, backoff: Duration) -> Self {
self.retry_backoff = backoff;
self
}
pub fn shutdown_timeout(mut self, timeout: Duration) -> Self {
self.shutdown_timeout = timeout;
self
}
pub fn enable_tracing(mut self, enable: bool) -> Self {
self.enable_tracing = enable;
self
}
pub fn dispatcher_config<F>(mut self, f: F) -> Self
where
F: FnOnce(DispatcherConfig) -> DispatcherConfig,
{
self.dispatcher = f(self.dispatcher);
self
}
pub fn handler_channel_size(mut self, size: usize) -> Self {
self.handler_channel_size = size;
self
}
pub fn dlq_channel_size(mut self, size: usize) -> Self {
self.dlq_channel_size = size;
self
}
pub fn wait_for_persistence(mut self, wait: bool) -> Self {
self.wait_for_persistence = wait;
self
}
}
impl EventBusConfig {
pub fn high_throughput() -> Self {
Self::default()
.dispatcher_config(|d| {
d.max_queue_size(50_000)
.worker_threads(num_cpus::get() * 2)
.drop_on_full(true)
.processing_timeout_ms(1000)
})
.max_retries(1)
.handler_channel_size(1024)
}
pub fn reliable() -> Self {
Self::default()
.dispatcher_config(|d| {
d.max_queue_size(10_000)
.drop_on_full(false)
.processing_timeout_ms(30_000)
})
.max_retries(5)
.retry_backoff(Duration::from_millis(500))
.handler_channel_size(512)
.dlq_channel_size(5000)
}
pub fn ordered() -> Self {
Self::default()
.dispatcher_config(|d| {
d.worker_threads(1)
.drop_on_full(false)
})
}
pub fn test() -> Self {
Self::default()
.dispatcher_config(|d| {
d.max_queue_size(100)
.worker_threads(2)
.processing_timeout_ms(1000)
})
.shutdown_timeout(Duration::from_secs(5))
.enable_tracing(false)
.handler_channel_size(64)
}
}