caio.co/de/foca

Add new, experimental, feture gated notifications

This patch introduces Notification::{DataSent,DataReceived} which
allows peeking into the interaction between peers without having
to deal with traces
Id
4680956925b4237432d1358b2a87ea555c28a573
Author
Caio
Commit time
2024-09-07T20:14:41+02:00

Modified CHANGELOG.md

@@ -11,6 +11,10
less worrisome for complex types
- Notifications can be converted to the new `foca::OwnedNotification`,
for convenience
+- New cargo feature `unstable-notification` which exposes the ability
+ to inspect cluster messages. This was written mostly so that users
+ can hook on the messages that expect a reply and measure round-trip
+ times

## v0.17.2 - 2024-06-04

Modified Cargo.toml

@@ -25,6 +25,8
bincode-codec = ["std", "serde", "bincode", "bytes/std"]
# Exposes `PostcardCodec`, a no_std-friendly codec
postcard-codec = ["serde", "postcard"]
+# Enables experimental message-related notifications
+unstable-notifications = []

[dependencies]
rand = { version = "0.8", default-features = false }

Modified README.md

@@ -86,6 +86,8
that uses [bincode][] under the hood.
* `postcard-codec`: Provides `PostcardCodec` a serde-based, `no_std`
friendly codec that uses [postcard][] under the hood.
+* `unstable-notifications`: Provides new notifications that allow
+ inspecting messages being sent and received


# Notes

Modified src/lib.rs

@@ -920,6 +920,9
}
}

+ #[cfg(feature = "unstable-notifications")]
+ runtime.notify(Notification::DataReceived(&header));
+
let Header {
src,
src_incarnation,
@@ -1522,6 +1525,8
tracing::trace!("Message sent");

runtime.send_to(dst, &data);
+ #[cfg(feature = "unstable-notifications")]
+ runtime.notify(Notification::DataSent(&header));

// absorb the buf into send_buf so we can reuse its capacity
debug_assert_eq!(0, self.send_buf.capacity(), "send_buf modified while taken");

Modified src/runtime.rs

@@ -6,6 +6,8

use bytes::{Bytes, BytesMut};

+#[cfg(feature = "unstable-notifications")]
+use crate::Header;
use crate::{Identity, Incarnation};

/// A Runtime is Foca's gateway to the real world: here is where
@@ -124,6 +126,18
/// This happens instead of `Defunct` when identities opt-in on
/// `Identity::renew()` functionality.
Rejoin(&'a T),
+
+ #[cfg(feature = "unstable-notifications")]
+ /// Foca successfully decoded data received via [`crate::Foca::handle_data`]
+ ///
+ /// See [`Header`]
+ DataReceived(&'a Header<T>),
+
+ #[cfg(feature = "unstable-notifications")]
+ /// Foca sent data to a peer
+ ///
+ /// See [`Header`]
+ DataSent(&'a Header<T>),
}

impl<'a, T> Notification<'a, T>
@@ -142,6 +156,10
Notification::Idle => OwnedNotification::Idle,
Notification::Defunct => OwnedNotification::Defunct,
Notification::Rejoin(id) => OwnedNotification::Rejoin(id.clone()),
+ #[cfg(feature = "unstable-notifications")]
+ Notification::DataReceived(h) => OwnedNotification::DataReceived(h.clone()),
+ #[cfg(feature = "unstable-notifications")]
+ Notification::DataSent(h) => OwnedNotification::DataSent(h.clone()),
}
}
}
@@ -165,6 +183,13
Defunct,
/// See [`Notification::Rejoin`]
Rejoin(T),
+ #[cfg(feature = "unstable-notifications")]
+ /// See [`Notification::DataReceived`]
+ DataReceived(Header<T>),
+
+ #[cfg(feature = "unstable-notifications")]
+ /// See [`Notification::DataSent`]
+ DataSent(Header<T>),
}

/// Timer is an event that's scheduled by a [`Runtime`]. You won't need