use bytes::Bytes;
use globset::{Glob, GlobSet, GlobSetBuilder};
use shep_core::protocol::{BusEvent, encode_frame};
use tokio::sync::broadcast::{self, error::RecvError};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
pub const BUS_CAPACITY: usize = 1024;
pub const MAX_TOPIC_PATTERNS: usize = 32;
#[must_use]
pub fn new_bus() -> broadcast::Sender<BusEvent> {
broadcast::channel(BUS_CAPACITY).0
}
#[derive(Debug)]
pub struct TopicFilter {
set: GlobSet,
#[allow(
dead_code,
reason = "read by this crate's own tests through `patterns`"
)]
patterns: Vec<String>,
}
impl TopicFilter {
pub fn new(patterns: &[String]) -> Result<Self, BusError> {
if patterns.len() > MAX_TOPIC_PATTERNS {
return Err(BusError::TooManyPatterns(patterns.len()));
}
let mut builder = GlobSetBuilder::new();
for pattern in patterns {
let glob = Glob::new(pattern).map_err(|e| BusError::BadPattern {
pattern: pattern.clone(),
message: e.to_string(),
})?;
builder.add(glob);
}
let set = builder.build().map_err(|e| BusError::BadPattern {
pattern: patterns.join(", "),
message: e.to_string(),
})?;
Ok(Self {
set,
patterns: patterns.to_vec(),
})
}
#[must_use]
pub fn matches(&self, event: &BusEvent) -> bool {
self.set.is_match(event.topic())
}
#[inline]
#[must_use]
#[allow(dead_code, reason = "called by this crate's own tests")]
pub fn patterns(&self) -> &[String] {
&self.patterns
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BusError {
BadPattern {
pattern: String,
message: String,
},
TooManyPatterns(usize),
}
impl core::fmt::Display for BusError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::BadPattern { pattern, message } => {
write!(f, "invalid topic pattern `{pattern}`: {message}")
}
Self::TooManyPatterns(count) => {
write!(
f,
"{count} topic patterns exceeds the limit of {MAX_TOPIC_PATTERNS}"
)
}
}
}
}
impl core::error::Error for BusError {}
enum Forwarded {
Frame(Bytes),
Skip,
Stop,
}
fn step(received: Result<BusEvent, RecvError>, filter: &TopicFilter) -> Forwarded {
let event = match received {
Ok(event) if filter.matches(&event) => event,
Ok(_) => return Forwarded::Skip,
Err(RecvError::Lagged(count)) => BusEvent::Dropped { count },
Err(RecvError::Closed) => return Forwarded::Stop,
};
match encode_frame(&event) {
Ok(bytes) => Forwarded::Frame(bytes),
Err(err) => {
tracing::warn!(%err, topic = event.topic(), "dropping an unencodable bus event");
Forwarded::Skip
}
}
}
pub fn spawn_forwarder(
mut rx: broadcast::Receiver<BusEvent>,
filter: TopicFilter,
out: mpsc::Sender<Bytes>,
) -> JoinHandle<()> {
tokio::spawn(async move {
loop {
match step(rx.recv().await, &filter) {
Forwarded::Frame(bytes) => {
if out.send(bytes).await.is_err() {
break; }
}
Forwarded::Skip => {}
Forwarded::Stop => break,
}
}
})
}
#[cfg(test)]
mod tests {
use super::*;
use shep_core::protocol::{BusEvent, ProcessEventKind, ProcessInfo, decode_frame};
use shep_core::status::ProcStatus;
fn filter(patterns: &[&str]) -> TopicFilter {
let owned: Vec<String> = patterns.iter().map(|p| (*p).to_string()).collect();
TopicFilter::new(&owned).unwrap()
}
fn process_event(id: u32, event: ProcessEventKind) -> BusEvent {
BusEvent::Process {
event,
info: ProcessInfo::builder(id, format!("sheep-{id}"), ProcStatus::Online)
.pid(Some(1000 + id))
.out_file(Some(format!("/logs/sheep-{id}-0-out.log")))
.err_file(Some(format!("/logs/sheep-{id}-0-err.log")))
.build(),
manually: false,
at_ms: 0,
}
}
#[test]
fn globs_match_the_dotted_topic_grammar() {
let processes = filter(&["process.*"]);
assert!(processes.matches(&process_event(0, ProcessEventKind::Exit)));
assert!(!processes.matches(&BusEvent::LogOut {
id: 0,
line: String::new()
}));
let logs = filter(&["log.out", "log.err"]);
assert!(logs.matches(&BusEvent::LogOut {
id: 0,
line: String::new()
}));
assert!(logs.matches(&BusEvent::LogErr {
id: 0,
line: String::new()
}));
assert!(!logs.matches(&BusEvent::DaemonShutdown));
let everything = filter(&["*"]);
assert!(everything.matches(&BusEvent::DaemonShutdown));
assert!(everything.matches(&process_event(1, ProcessEventKind::Start)));
}
#[test]
fn an_empty_topic_list_matches_nothing() {
let none = TopicFilter::new(&[]).unwrap();
assert!(!none.matches(&BusEvent::DaemonShutdown));
assert!(none.patterns().is_empty());
}
#[test]
fn a_bad_pattern_is_a_typed_error_carrying_the_pattern() {
let err = TopicFilter::new(&["process.[".to_string()]).unwrap_err();
assert!(matches!(err, BusError::BadPattern { ref pattern, .. } if pattern == "process.["));
}
#[test]
fn too_many_patterns_are_refused_with_the_count() {
let many: Vec<String> = (0..=MAX_TOPIC_PATTERNS).map(|i| format!("t{i}")).collect();
assert_eq!(
TopicFilter::new(&many).unwrap_err(),
BusError::TooManyPatterns(MAX_TOPIC_PATTERNS + 1)
);
}
#[tokio::test(start_paused = true)]
async fn lag_becomes_a_dropped_notice_that_bypasses_the_filter() {
let (tx, mut rx) = tokio::sync::broadcast::channel(4);
for id in 0..10 {
tx.send(process_event(id, ProcessEventKind::Start)).unwrap();
}
let missed = match rx.recv().await {
Err(RecvError::Lagged(n)) => n,
other => panic!("expected a lag after overflowing the ring, got {other:?}"),
};
assert!(missed > 0);
let Forwarded::Frame(bytes) = step(Err(RecvError::Lagged(missed)), &filter(&["process.*"]))
else {
panic!("a lag must always produce a Dropped frame")
};
assert_eq!(
decode_frame::<BusEvent>(&bytes).unwrap(),
BusEvent::Dropped { count: missed }
);
}
#[tokio::test(start_paused = true)]
async fn forwarder_delivers_only_matching_frames_then_closes_with_the_bus() {
let (tx, rx) = tokio::sync::broadcast::channel(16);
let (out_tx, mut out_rx) = tokio::sync::mpsc::channel(16);
let handle = spawn_forwarder(rx, filter(&["process.*"]), out_tx);
tx.send(process_event(0, ProcessEventKind::Start)).unwrap();
tx.send(BusEvent::LogOut {
id: 0,
line: "noise".to_string(),
})
.unwrap();
tx.send(process_event(0, ProcessEventKind::Online)).unwrap();
drop(tx);
handle.await.unwrap();
let first: BusEvent = decode_frame(&out_rx.recv().await.unwrap()).unwrap();
let second: BusEvent = decode_frame(&out_rx.recv().await.unwrap()).unwrap();
assert!(matches!(
first,
BusEvent::Process {
event: ProcessEventKind::Start,
..
}
));
assert!(matches!(
second,
BusEvent::Process {
event: ProcessEventKind::Online,
..
}
));
assert!(
out_rx.recv().await.is_none(),
"forwarder must drop its sender at bus close"
);
}
#[tokio::test(start_paused = true)]
async fn forwarder_stops_when_the_subscriber_hangs_up() {
let (tx, rx) = tokio::sync::broadcast::channel(16);
let (out_tx, out_rx) = tokio::sync::mpsc::channel(1);
let handle = spawn_forwarder(rx, filter(&["*"]), out_tx);
drop(out_rx);
tx.send(BusEvent::DaemonShutdown).unwrap();
handle.await.unwrap(); }
}