WIP
This commit is contained in:
+29
-23
@@ -1,7 +1,10 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::VecDeque, sync::{RwLock, mpsc::{Receiver, Sender}},
|
collections::{HashMap, VecDeque}, sync::RwLock, thread::{self, JoinHandle}, time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub type EventId = u64;
|
||||||
|
pub type SubscriptionId = u64;
|
||||||
|
|
||||||
pub enum HError {
|
pub enum HError {
|
||||||
BusLockPoisoned(String),
|
BusLockPoisoned(String),
|
||||||
}
|
}
|
||||||
@@ -15,26 +18,15 @@ pub struct Event {
|
|||||||
pub struct EventBus {
|
pub struct EventBus {
|
||||||
queue: RwLock<VecDeque<Event>>,
|
queue: RwLock<VecDeque<Event>>,
|
||||||
subscriptions: RwLock<Vec<Subscription>>,
|
subscriptions: RwLock<Vec<Subscription>>,
|
||||||
processor: Sender<EventProcessRequest>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait SubscriptionEndpoint {
|
pub struct SubscriptionRequest {
|
||||||
fn deliver(&self, evt: &Event);
|
topic_pattern: String,
|
||||||
}
|
|
||||||
|
|
||||||
struct ConsoleEndpoint;
|
|
||||||
impl SubscriptionEndpoint for ConsoleEndpoint {
|
|
||||||
fn deliver(&self, evt: &Event) {
|
|
||||||
let data = String::from_utf8_lossy(&evt.data_utf8);
|
|
||||||
println!("({}, {}, {})", evt.id, evt.topic_name, data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Subscription {
|
pub struct Subscription {
|
||||||
topic_pattern: String,
|
id: u64,
|
||||||
/// from_id is inclusive
|
request: SubscriptionRequest,
|
||||||
from_id: u64,
|
|
||||||
endpoint: Box<dyn SubscriptionEndpoint>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EventBus {
|
impl EventBus {
|
||||||
@@ -54,23 +46,37 @@ impl EventBus {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subscribe(&self, request: Subscription) -> Result<(), HError> {
|
pub fn subscribe(&self, request: SubscriptionRequest) -> Result<(), HError> {
|
||||||
let mut subscriptions = self
|
let mut subscriptions = self
|
||||||
.subscriptions
|
.subscriptions
|
||||||
.write()
|
.write()
|
||||||
.map_err(|e| HError::BusLockPoisoned(e.to_string()))?;
|
.map_err(|e| HError::BusLockPoisoned(e.to_string()))?;
|
||||||
subscriptions.push(request);
|
|
||||||
|
let last_id = subscriptions.last().map(|s| s.id).unwrap_or(0);
|
||||||
|
let subscription = Subscription {
|
||||||
|
id: last_id + 1,
|
||||||
|
request,
|
||||||
|
};
|
||||||
|
subscriptions.push(subscription);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct EventProcessRequest {
|
struct EventBusProcessor {
|
||||||
id: u64,
|
last_id: u64,
|
||||||
|
subscriptions_queue: RwLock<HashMap<SubscriptionId, EventId>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct EventBusProcessor {
|
impl EventBusProcessor {
|
||||||
last_processed_id: u64,
|
fn start() -> JoinHandle<u64> {
|
||||||
work: Receiver<u64>,
|
thread::spawn(|| {
|
||||||
|
loop {
|
||||||
|
thread::sleep(Duration::from_secs(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in New Issue
Block a user