1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
//! Contains the [`EvidentPublisher`] struct that is used to create public static publishers.
//!
//! Use the [`create_static_publisher`](crate::create_static_publisher) macro for a convenience wrapper to create a publisher.
//!
//! [req:pub]
use std::{
collections::{HashMap, HashSet},
sync::{
atomic::{AtomicBool, AtomicUsize, Ordering},
mpsc::{self, SyncSender, TrySendError},
Arc, RwLock,
},
thread,
};
use crate::{
event::{entry::EventEntry, filter::Filter, intermediary::IntermediaryEvent, Event, Id, Msg},
subscription::{Subscription, SubscriptionError, SubscriptionSender},
this_origin,
};
/// Trait to implement for [`Id`], to control the publisher and all listeners.
///
/// [req:cap.ctrl]
pub trait CaptureControl {
/// Returns `true` if the given [`Id`] is used to signal the start of event capturing.
///
/// **Possible implementation:**
///
/// ```ignore
/// id == &START_CAPTURING_ID
/// ```
///
/// [req:cap.ctrl.start]
fn start(id: &Self) -> bool;
/// Returns the *start-ID*.
///
/// [req:cap.ctrl.start]
fn start_id() -> Self;
/// Returns `true` if the given [`Id`] is used to signal the end of event capturing.
///
/// **Possible implementation:**
///
/// ```ignore
/// id == &STOP_CAPTURING_ID
/// ```
///
/// [req:cap.ctrl.stop]
fn stop(id: &Self) -> bool;
/// Returns the *stop-ID*.
///
/// [req:cap.ctrl.stop]
fn stop_id() -> Self;
}
/// Returns `true` if the given [`Id`] is used to control capturing.
///
/// [req:cap.ctrl]
pub fn is_control_id(id: &impl CaptureControl) -> bool {
CaptureControl::stop(id) || CaptureControl::start(id)
}
/// Defines the capture mode for a publisher.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CaptureMode {
/// Event capturing may be blocking if the capture buffer is full.
Blocking,
/// Event capturing does not block, resulting in events **not** being captured if the capture buffer is full.
///
/// You may inspect the number of missed events with the `get_missed_captures()` of the [`EvidentPublisher`].
NonBlocking,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EventTimestampKind {
/// Sets the event time, when the event is captured.
///
/// **Note:** With this setting, event timestamps might show incorrect order in case of concurrent events, because events are buffered before capturing.
///
/// **Note:** This has slightly better performance on the thread setting an event, because system time access is delayed to the capturing thread.
Captured,
/// Sets the event time, when the event is created.
///
/// **Note:** This has slightly worse performance on the thread setting an event, because system time access most likely requires a context switch.
Created,
}
// Types below used for better clarity according to clippy.
type Subscriber<K, M, T> = HashMap<crate::uuid::Uuid, SubscriptionSender<K, M, T>>;
type IdSubscriber<K, M, T> = HashMap<K, Subscriber<K, M, T>>;
type Capturer<K, M, T> = SyncSender<Event<K, M, T>>;
/// An **EvidentPublisher** is used to capture, publish, and manage subscriptions.
///
/// [req:pub]
pub struct EvidentPublisher<K, M, T, F>
where
K: Id + CaptureControl,
M: Msg,
T: EventEntry<K, M>,
F: Filter<K, M>,
{
/// The hashmap of subscribers listening to specific events.
///
/// [req:subs.specific]
pub(crate) subscriptions: Arc<RwLock<IdSubscriber<K, M, T>>>,
/// The hashmap of subscribers listening to all events.
///
/// [req:subs.all]
pub(crate) any_event: Arc<RwLock<Subscriber<K, M, T>>>,
/// The send-part of the capturing channel.
///
/// [req:cap]
pub(crate) capturer: Capturer<K, M, T>,
/// Optional filter that is applied when capturing events.
///
/// [req:cap.filter]
filter: Option<F>,
/// Flag to control if capturing is active or inactive.
///
/// [req:cap.ctrl]
capturing: Arc<AtomicBool>,
/// Flag to control the capture mode.
capture_blocking: Arc<AtomicBool>,
/// Defines the size of the capturing send-buffer.
///
/// [req:cap]
capture_channel_bound: usize,
/// Defines the size of each subscription send-buffer.
///
/// [req:subs]
subscription_channel_bound: usize,
/// Number of missed captures in *non-blocking* capture mode.
missed_captures: Arc<AtomicUsize>,
/// Defines at what point the event-timestamp is created.
timestamp_kind: EventTimestampKind,
}
impl<K, M, T, F> EvidentPublisher<K, M, T, F>
where
K: Id + CaptureControl,
M: Msg,
T: EventEntry<K, M>,
F: Filter<K, M>,
{
/// Create a new [`EvidentPublisher`], and spawn a new event handler thread for events captured by the publisher.
///
/// **Note:** You should use the macro [`create_static_publisher`](crate::create_static_publisher) instead.
///
/// [req:pub]
fn create(
mut on_event: impl FnMut(Event<K, M, T>) + std::marker::Send + 'static,
filter: Option<F>,
capture_mode: CaptureMode,
capture_channel_bound: usize,
subscription_channel_bound: usize,
timestamp_kind: EventTimestampKind,
) -> Self {
let (send, recv): (SyncSender<Event<K, M, T>>, _) =
mpsc::sync_channel(capture_channel_bound);
// [req:pub.threaded]
thread::spawn(move || {
while let Ok(mut event) = recv.recv() {
if timestamp_kind == EventTimestampKind::Captured {
event.timestamp = Some(std::time::SystemTime::now());
}
on_event(event);
}
});
let mode = match capture_mode {
CaptureMode::Blocking => Arc::new(AtomicBool::new(true)),
CaptureMode::NonBlocking => Arc::new(AtomicBool::new(false)),
};
EvidentPublisher {
subscriptions: Arc::new(RwLock::new(HashMap::new())),
any_event: Arc::new(RwLock::new(HashMap::new())),
capturer: send,
filter,
// [req:cap.ctrl.init]
capturing: Arc::new(AtomicBool::new(true)),
capture_blocking: mode,
capture_channel_bound,
subscription_channel_bound,
missed_captures: Arc::new(AtomicUsize::new(0)),
timestamp_kind,
}
}
/// Create a new [`EvidentPublisher`] without an event filter.
///
/// **Note:** You should use the macro [`create_static_publisher`](crate::create_static_publisher) instead.
///
/// [req:pub]
pub fn new(
on_event: impl FnMut(Event<K, M, T>) + std::marker::Send + 'static,
capture_mode: CaptureMode,
capture_channel_bound: usize,
subscription_channel_bound: usize,
time_stamp_kind: EventTimestampKind,
) -> Self {
Self::create(
on_event,
None,
capture_mode,
capture_channel_bound,
subscription_channel_bound,
time_stamp_kind,
)
}
/// Create a new [`EvidentPublisher`] with an event filter.
///
/// **Note:** You should use the macro [`create_static_publisher`](crate::create_static_publisher) instead.
///
/// [req:pub], [req:cap.filter]
pub fn with(
on_event: impl FnMut(Event<K, M, T>) + std::marker::Send + 'static,
filter: F,
capture_mode: CaptureMode,
capture_channel_bound: usize,
subscription_channel_bound: usize,
timestamp_kind: EventTimestampKind,
) -> Self {
Self::create(
on_event,
Some(filter),
capture_mode,
capture_channel_bound,
subscription_channel_bound,
timestamp_kind,
)
}
/// Returns the event filter, or `None` if no filter is set.
///
/// [req:cap.filter]
pub fn get_filter(&self) -> &Option<F> {
&self.filter
}
/// Returns `true` if the given event-entry passes the filter, or the event-ID is a control-ID.
///
/// [req:cap.filter]
pub fn entry_allowed(&self, entry: &impl EventEntry<K, M>) -> bool {
if !is_control_id(entry.get_event_id()) {
if !self.capturing.load(Ordering::Acquire) {
return false;
}
if let Some(filter) = &self.filter {
if !filter.allow_entry(entry) {
return false;
}
}
}
true
}
/// Captures an intermediary event, and sends the resulting event to the event handler.
///
/// **Note:** This function should **not** be called manually, because it is automatically called on `drop()` of an intermediary event.
///
/// [req:cap]
#[doc(hidden)]
pub fn _capture<I: IntermediaryEvent<K, M, T>>(&self, interm_event: &mut I) {
let entry = interm_event.take_entry();
// [req:cap.filter]
if !self.entry_allowed(&entry) {
return;
}
let mut event = Event::new(entry);
if self.timestamp_kind == EventTimestampKind::Created {
event.timestamp = Some(std::time::SystemTime::now());
}
if self.capture_blocking.load(Ordering::Acquire) {
let _ = self.capturer.send(event);
} else {
let res = self.capturer.try_send(event);
if let Err(TrySendError::Full(_)) = res {
// Note: If another thread has missed captures at the same moment, the count may be inaccurate, because there is no lock.
// This should still be fine, since
// - highly unlikely to happen during production with reasonable channel bounds and number of logs captured
// - count is still increased, and any increase in missed captures is bad (+/- one or two is irrelevant)
let missed_captures = self.missed_captures.load(Ordering::Relaxed);
if missed_captures < usize::MAX {
self.missed_captures
.store(missed_captures + 1, Ordering::Relaxed);
}
}
}
}
/// Returns the current capture mode.
pub fn get_capture_mode(&self) -> CaptureMode {
if self.capture_blocking.load(Ordering::Acquire) {
CaptureMode::Blocking
} else {
CaptureMode::NonBlocking
}
}
/// Allows to change the capture mode.
pub fn set_capture_mode(&self, mode: CaptureMode) {
match mode {
CaptureMode::Blocking => self.capture_blocking.store(true, Ordering::Release),
CaptureMode::NonBlocking => self.capture_blocking.store(false, Ordering::Release),
}
}
/// Returns the number of missed captures in *non-blocking* mode since last reset.
pub fn get_missed_captures(&self) -> usize {
self.missed_captures.load(Ordering::Relaxed)
}
/// Resets the number of missed captures in *non-blocking* mode.
pub fn reset_missed_captures(&self) {
self.missed_captures.store(0, Ordering::Relaxed);
}
/// Returns a subscription to events with the given event-ID,
/// or a [`SubscriptionError<K>`] if the subscription could not be created.
///
/// [req:subs.specific.one]
pub fn subscribe(&self, id: K) -> Result<Subscription<K, M, T, F>, SubscriptionError<K>> {
self.subscribe_to_many(vec![id])
}
/// Returns a subscription to events with the given event-IDs,
/// or a [`SubscriptionError<K>`] if the subscription could not be created.
///
/// [req:subs.specific.mult]
pub fn subscribe_to_many(
&self,
ids: Vec<K>,
) -> Result<Subscription<K, M, T, F>, SubscriptionError<K>> {
// Note: Number of ids to listen to most likely affects the number of received events => number is added to channel bound
// Addition instead of multiplication, because even distribution accross events is highly unlikely.
let (sender, receiver) = mpsc::sync_channel(ids.len() + self.subscription_channel_bound);
let channel_id = crate::uuid::Uuid::new_v4();
let subscription_sender = SubscriptionSender { channel_id, sender };
match self.subscriptions.write().ok() {
Some(mut locked_subs) => {
for id in ids.clone() {
let entry = locked_subs.entry(id.clone());
entry
.and_modify(|v| {
v.insert(subscription_sender.channel_id, subscription_sender.clone());
})
.or_insert({
let mut h = HashMap::new();
h.insert(subscription_sender.channel_id, subscription_sender.clone());
h
});
}
}
None => {
return Err(SubscriptionError::CouldNotAccessPublisher);
}
}
Ok(Subscription {
channel_id,
receiver,
sub_to_all: false,
subscriptions: Some(HashSet::from_iter(ids)),
publisher: self,
})
}
/// Returns a subscription to all events,
/// or a [`SubscriptionError<K>`] if the subscription could not be created.
///
/// [req:subs.all]
pub fn subscribe_to_all_events(
&self,
) -> Result<Subscription<K, M, T, F>, SubscriptionError<K>> {
let (sender, receiver) = mpsc::sync_channel(self.capture_channel_bound);
let channel_id = crate::uuid::Uuid::new_v4();
match self.any_event.write().ok() {
Some(mut locked_vec) => {
locked_vec.insert(channel_id, SubscriptionSender { channel_id, sender });
}
None => {
return Err(SubscriptionError::CouldNotAccessPublisher);
}
}
Ok(Subscription {
channel_id,
receiver,
sub_to_all: true,
subscriptions: None,
publisher: self,
})
}
/// Returns `true` if capturing is *active*.
///
/// [req:cap.ctrl.info]
pub fn is_capturing(&self) -> bool {
self.capturing.load(Ordering::Acquire)
}
/// Start capturing.
///
/// **Note:** Capturing is already started initially, so this function is only needed after manually stopping capturing.
///
/// [req:cap.ctrl.start]
pub fn start(&self) {
let empty_msg: Option<M> = None;
let start_event = Event::new(EventEntry::new(K::start_id(), empty_msg, this_origin!()));
let _ = self.capturer.send(start_event);
self.capturing.store(true, Ordering::Release);
}
/// Stop capturing.
///
/// [req:cap.ctrl.stop]
pub fn stop(&self) {
let empty_msg: Option<M> = None;
let stop_event = Event::new(EventEntry::new(K::stop_id(), empty_msg, this_origin!()));
let _ = self.capturer.send(stop_event);
self.capturing.store(false, Ordering::Release);
}
/// Send the given event to all subscriber of the event.
///
/// **Note:** This function should **not** be called manually, because it is already called in the event handler.
///
/// [req:cap]
#[doc(hidden)]
pub fn on_event(&self, event: Event<K, M, T>) {
let arc_event = Arc::new(event);
let key = arc_event.entry.get_event_id();
let mut bad_subs: Vec<crate::uuid::Uuid> = Vec::new();
let mut bad_any_event: Vec<crate::uuid::Uuid> = Vec::new();
if let Ok(locked_subscriptions) = self.subscriptions.read() {
if let Some(sub_senders) = locked_subscriptions.get(key) {
for (channel_id, sub_sender) in sub_senders.iter() {
let bad_channel = if self.capture_blocking.load(Ordering::Acquire) {
sub_sender.sender.send(arc_event.clone()).is_err()
} else {
matches!(
sub_sender.sender.try_send(arc_event.clone()),
Err(TrySendError::Disconnected(_))
)
};
if bad_channel {
bad_subs.push(*channel_id);
}
}
}
}
if let Ok(locked_vec) = self.any_event.read() {
for (channel_id, any_event_sender) in locked_vec.iter() {
let bad_channel = if self.capture_blocking.load(Ordering::Acquire) {
any_event_sender.sender.send(arc_event.clone()).is_err()
} else {
matches!(
any_event_sender.sender.try_send(arc_event.clone()),
Err(TrySendError::Disconnected(_))
)
};
if bad_channel {
bad_any_event.push(*channel_id);
}
}
}
// Remove dead channels
if !bad_subs.is_empty() {
if let Ok(mut locked_subscriptions) = self.subscriptions.write() {
let mut entry = locked_subscriptions.entry(key.clone());
for i in bad_subs {
entry = entry.and_modify(|v| {
v.remove(&i);
});
}
}
}
if !bad_any_event.is_empty() {
if let Ok(mut locked_vec) = self.any_event.write() {
for i in bad_any_event {
locked_vec.remove(&i);
}
}
}
}
}