Repository navigation
Overhaul stats: Improve events package (part 3) - #1486
josecelano merged 9 commits into
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1486 +/- ##
===========================================
- Coverage 84.36% 84.32% -0.04%
===========================================
Files 250 251 +1
Lines 19719 19821 +102
Branches 19719 19821 +102
===========================================
+ Hits 16636 16715 +79
- Misses 2779 2798 +19
- Partials 304 308 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
5cded47 to
8f5b57e
Compare
…nder> to avoid double allocations
4e8d733 to
d98d93f
Compare
|
ACK d98d93f |
da2ce7
left a comment
There was a problem hiding this comment.
This is a post-merge review of the change from #1486 as it stands at a94c3ced8 on the default branch.
What still runs: the Receiver trait and the crate-owned SendError and RecvError types in packages/events, the broadcast-channel implementations of Sender and Receiver with the conversions from the tokio errors, EventBus::sender() returning Option<Arc<dyn Sender>>, EventBus::receiver() returning Box<dyn Receiver>, and the event::{sender, receiver, bus} aliases in http-core, udp-core and udp-server. Every statistics listener and the UDP banning listener consume events through this trait, so its contract reaches ban handling.
[Minor] The documented outcomes of Sender::send do not match the only implementation. packages/events/src/sender.rs:19-22 says None means the event was not sent and gives no active receivers as an example, but Broadcaster::send (packages/events/src/broadcaster.rs:33-35) always returns Some, and with no subscribed receiver it returns Some(Err(SendError(event))). The change's own test it_should_fail_when_trying_tos_send_with_no_subscribers (packages/events/src/broadcaster.rs:110-119) asserts exactly that. Concretely, EventBus::default().sender().unwrap().send(e).await before any receiver() call yields Some(Err(..)), and its Display (packages/events/src/sender.rs:33-37) reads channel closed although the bus and its sender are alive. A consumer that matches None to detect that nobody is listening never takes that branch. The disabled case is already expressed one level up, by EventBus::sender() returning None (packages/events/src/bus.rs:56-61). Smallest fix: document Some(Err(SendError(event))) as no receiver subscribed, with the event handed back; document None as reserved for implementations that drop the event on purpose, which the broadcaster never does; and have the Display text say no active receivers.
[Minor] Receiver::recv (packages/events/src/receiver.rs:12) does not say it must be cancel safe, and every listener depends on that. packages/udp-server/src/banning/event/listener.rs:93, packages/udp-server/src/statistics/event/listener.rs:81, packages/udp-core/src/statistics/event/listener.rs:80, packages/http-core/src/statistics/event/listener.rs:83, packages/tracker-core/src/statistics/event/listener.rs:113 and :147, and packages/swarm-coordination-registry/src/statistics/event/listener.rs:65 all race receiver.recv() against cancellation_token.cancelled() in a biased tokio::select!. Each loop iteration creates the recv future and drops it whenever cancellation wins, and the listener then drains with try_recv. An implementation that takes the next event before its returned future completes, either in the synchronous part of recv or before a later await point, loses that event on cancellation, and the drain never sees it. On the banning path that is one uncounted connection-cookie error. The broadcast implementation (packages/events/src/broadcaster.rs:41-43) is cancel safe because tokio documents broadcast::Receiver::recv as cancel safe, so nothing is lost at this head. The test double at packages/udp-server/src/banning/event/listener.rs:190-191 already has a comment working the rule out again. Smallest fix: add a # Cancel safety section to Receiver::recv saying that dropping the returned future before it completes must not consume an event.
[Nit] The description says the traits were decoupled from the tokio broadcast implementation, but Broadcaster::subscribe (packages/events/src/broadcaster.rs:25) still returns tokio::sync::broadcast::Receiver<Event>, and EventBus::broadcaster is a public field (packages/events/src/bus.rs:34). Any package can therefore call bus.broadcaster.subscribe() and bypass the Receiver trait. At this head only tests do so (packages/udp-server/src/handlers/scrape.rs:403, packages/udp-server/src/handlers/error.rs:254). Smallest fix: return Box<dyn Receiver<Event = Event>> from subscribe, as EventBus::receiver does, or make it pub(crate), and make the field private.
Checked and found sound:
- The
Fromconversions mapClosedandLagged(n)one to one (packages/events/src/broadcaster.rs:54-77). self.recv()inside the trait'srecv(packages/events/src/broadcaster.rs:42) calls tokio's inherent method, not itself, because inherent methods take precedence over trait methods.Option<Arc<dyn Sender>>removes the double allocation the description promised.- Every listener stops on
Closedand warns and continues onLagged. - The UDP-server listeners subscribe when bootstrap builds their runners (
src/app.rs:477-481), before the UDP instances start, and the UDP instances stop before those listeners at shutdown. So ignoring the result ofsendat the call sites, for examplepackages/udp-server/src/server/launcher.rs:465, loses no event while publishers run. - At merge, only tests needed the
timefeature this change added to the normal tokio dependency. It is now used in production bypackages/events/src/drain.rs:16.
Continuation of #1483
Subtasks
Receivertrait.SenderandReceivertraits from the tokio broadcast implementation. Wrapper forSendErrorandRecvError.Option<Arc<...>>instead of anArc<Option<...>>in theEventBusimplementation.Arc<Box<dyn Sender>>withArc<dyn Sender>to avoid double allocations.