Skip to content

Commit 7e364d1

Browse files
committed
refactor: [torrust#1396] move event channel creation to events mod in HTTP tracker core
1 parent 3d2243b commit 7e364d1

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

packages/http-tracker-core/src/event/sender.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,35 @@ use tokio::sync::broadcast::error::SendError;
77

88
use super::Event;
99

10-
/// A trait to allow sending events.
10+
const CHANNEL_CAPACITY: usize = 1024;
11+
12+
/// A trait for sending sending.
1113
#[cfg_attr(test, automock)]
1214
pub trait Sender: Sync + Send {
1315
fn send_event(&self, event: Event) -> BoxFuture<'_, Option<Result<usize, SendError<Event>>>>;
1416
}
1517

1618
/// An event sender implementation using a broadcast channel.
17-
#[allow(clippy::module_name_repetitions)]
18-
pub struct ChannelSender {
19+
pub struct Broadcaster {
1920
pub(crate) sender: broadcast::Sender<Event>,
2021
}
2122

22-
impl Sender for ChannelSender {
23+
impl Sender for Broadcaster {
2324
fn send_event(&self, event: Event) -> BoxFuture<'_, Option<Result<usize, SendError<Event>>>> {
2425
async move { Some(self.sender.send(event)) }.boxed()
2526
}
2627
}
28+
29+
impl Default for Broadcaster {
30+
fn default() -> Self {
31+
let (sender, _) = broadcast::channel(CHANNEL_CAPACITY);
32+
Self { sender }
33+
}
34+
}
35+
36+
impl Broadcaster {
37+
#[must_use]
38+
pub fn subscribe(&self) -> broadcast::Receiver<Event> {
39+
self.sender.subscribe()
40+
}
41+
}

packages/http-tracker-core/src/statistics/setup.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,36 @@
11
//! Setup for the tracker statistics.
22
//!
33
//! The [`factory`] function builds the structs needed for handling the tracker metrics.
4-
use tokio::sync::broadcast;
5-
6-
use crate::event::sender::ChannelSender;
4+
use crate::event::sender::Broadcaster;
75
use crate::{event, statistics};
86

9-
const CHANNEL_CAPACITY: usize = 1024;
10-
117
/// It builds the structs needed for handling the tracker metrics.
128
///
139
/// It returns:
1410
///
15-
/// - An statistics event [`Sender`](crate::statistics::event::sender::Sender) that allows you to send events related to statistics.
16-
/// - An statistics [`Repository`](crate::statistics::repository::Repository) which is an in-memory repository for the tracker metrics.
11+
/// - An event [`Sender`](crate::event::sender::Sender) that allows you to send
12+
/// events related to statistics.
13+
/// - An statistics [`Repository`](crate::statistics::repository::Repository)
14+
/// which is an in-memory repository for the tracker metrics.
1715
///
18-
/// When the input argument `tracker_usage_statistics`is false the setup does not run the event listeners, consequently the statistics
19-
/// events are sent are received but not dispatched to the handler.
16+
/// When the input argument `tracker_usage_statistics`is false the setup does
17+
/// not run the event listeners, consequently the statistics events are sent are
18+
/// received but not dispatched to the handler.
2019
#[must_use]
2120
pub fn factory(tracker_usage_statistics: bool) -> (Option<Box<dyn event::sender::Sender>>, statistics::repository::Repository) {
22-
let mut stats_event_sender: Option<Box<dyn event::sender::Sender>> = None;
23-
2421
let mut keeper = statistics::keeper::Keeper::new();
2522

26-
if tracker_usage_statistics {
27-
let (sender, _) = broadcast::channel(CHANNEL_CAPACITY);
23+
let opt_event_sender: Option<Box<dyn event::sender::Sender>> = if tracker_usage_statistics {
24+
let broadcaster = Broadcaster::default();
2825

29-
let receiver = sender.subscribe();
26+
keeper.run_event_listener(broadcaster.subscribe());
3027

31-
stats_event_sender = Some(Box::new(ChannelSender { sender }));
32-
33-
keeper.run_event_listener(receiver);
34-
}
28+
Some(Box::new(broadcaster))
29+
} else {
30+
None
31+
};
3532

36-
(stats_event_sender, keeper.repository)
33+
(opt_event_sender, keeper.repository)
3734
}
3835

3936
#[cfg(test)]

0 commit comments

Comments
 (0)