Labels
Code Cleanup / RefactoringTidying and Making Neat
Description
All the duplicated code related to events has been extracted into a new workspace package.
- Dir:
packages/events - Crate name:
torrust-tracker-events
Now we should make some improvements before using the package in more packages, such as the torrent-repository package.
Subtasks
Receivertrait.- Decouple
SenderandReceivertraits from the tokio broadcast implementation. - Return an
Option<Arc<...>>instead of anArc<Option<...>>in theEventBusimplementation. - Replace
Arc<Box<dyn Sender>>withArc<dyn Sender>to avoid double allocations. - Add unit tests.
Decouple Sender and Receiver traits from tokio broadcast implementation
The error SendError is a broadcast-specific tokio::sync::broadcast::error.
pub trait Sender: Sync + Send {
type Event: Send + Clone;
fn send_event(&self, event: Self::Event) -> BoxFuture<'_, Option<Result<usize, SendError<Self::Event>>>>;
}We should wrap that error in our own error type and not expose the internal tokio error.
Return an Option<Arc<...>> instead of an Arc<Option<...>> in the EventBus implementation
Currently:
impl<E: Sync + Send + Clone + 'static> EventBus<E> {
// ...
#[must_use]
pub fn sender(&self) -> Arc<Option<Box<dyn sender::Sender<Event = E>>>> {
if self.enable_sender {
Arc::new(Some(Box::new(self.broadcaster.clone())))
} else {
Arc::new(None)
}
}
// ...We propose changing it to:
#[must_use]
pub fn sender(&self) -> Option<Arc<dyn sender::Sender<Event = E>>> {
if self.enable_sender {
Some(Arc::new(self.broadcaster.clone()))
} else {
None
}
}This change makes the optionality clearer, avoids unnecessary heap allocations when enable_sender is false, and simplifies usage.
Example usage:
if let Some(sender) = event_bus.sender() {
sender.send(my_event);
}Additionally, by returning Arc<dyn Sender> directly (without a Box), we avoid an extra heap allocation, making the design more efficient and clean.
Metadata
Metadata
Assignees
Labels
Code Cleanup / RefactoringTidying and Making Neat
Activity
fix: [torrust#1485] remove duplicate const
feat: [torrust#1485] add Receiver trait to events package
refactor: [torrust#1485] decouple events traits from tokio broadcast …
refactor: [torrust#1485] normalize event type parameter name in event…
refactor: [torrust#1485] rename trait fn send_event to send
refactor: [torrust#1485] rename trait fn send_event to send
refactor: [torrust#1485] replace Arc<Option> with Option<Arc> in even…
refactor: [torrust#1485] replace Arc<Box<dyn Sender>> with Arc<dyn Se…
test: [torrust#1485] add unit tests to events pkg
test: [torrust#1485] add unit tests to events pkg
refactor: [torrust#1485] simplify event mods in pakages using the eve…
Merge #1486: Overhaul stats: Improve events package (part 3)