pub struct GossipProtocolEngine { /* private fields */ }Expand description
Production-quality gossip dissemination protocol engine.
The engine is not async; it exposes a pure, synchronous API suitable for
embedding inside higher-level async event loops. All state is owned by the
struct and protected via &mut self.
Implementations§
Source§impl GossipProtocolEngine
impl GossipProtocolEngine
Sourcepub fn new(config: GossipConfig) -> Self
pub fn new(config: GossipConfig) -> Self
Create a new engine with the given configuration.
The PRNG seed is derived from dedup_cache_size XOR a constant so that
default-config engines are reproducible in tests.
Sourcepub fn with_defaults() -> Self
pub fn with_defaults() -> Self
Create an engine with default configuration.
Sourcepub fn add_peer(&mut self, peer_id: String) -> Result<(), EngineError>
pub fn add_peer(&mut self, peer_id: String) -> Result<(), EngineError>
Register a new peer. Returns Ok(()) if the peer was added, or if it
was already present (idempotent).
Sourcepub fn remove_peer(&mut self, peer_id: &str) -> Result<(), EngineError>
pub fn remove_peer(&mut self, peer_id: &str) -> Result<(), EngineError>
Remove a registered peer. Returns Err(PeerNotFound) if absent.
Sourcepub fn create_message(
payload: Vec<u8>,
topic: String,
origin: String,
timestamp: u64,
max_hops: u8,
) -> Result<GossipMessage, EngineError>
pub fn create_message( payload: Vec<u8>, topic: String, origin: String, timestamp: u64, max_hops: u8, ) -> Result<GossipMessage, EngineError>
Create a new GossipMessage with a computed FNV-1a ID.
timestamp should be a monotonically-increasing value (e.g.
SystemTime::now() milliseconds); when running tests without a real
clock, pass any non-zero value.
Sourcepub fn receive_message(
&mut self,
msg: GossipMessage,
) -> Result<Vec<GossipEvent>, EngineError>
pub fn receive_message( &mut self, msg: GossipMessage, ) -> Result<Vec<GossipEvent>, EngineError>
Process an incoming GossipMessage.
Returns a Vec<GossipEvent> describing what happened:
DuplicateDroppedif the message was already seen.MessageReceived+MessageForwardedif accepted and forwarded.MessageReceivedonly if there are no eligible forward peers.
Sourcepub fn select_peers(
&mut self,
exclude: &str,
strategy: &FanoutStrategy,
) -> Vec<String>
pub fn select_peers( &mut self, exclude: &str, strategy: &FanoutStrategy, ) -> Vec<String>
Select peers to forward a message to, applying the given strategy.
The returned list excludes exclude (the origin peer) and only
contains active peers.
Sourcepub fn update_peer_score(&mut self, peer_id: &str, delivered: bool)
pub fn update_peer_score(&mut self, peer_id: &str, delivered: bool)
Update a peer’s fanout_score using an EMA with α = 0.1.
delivered = true → positive signal (score nudges toward 1.0).
delivered = false → negative signal (score nudges toward 0.0).
Sourcepub fn pending_messages(&self) -> usize
pub fn pending_messages(&self) -> usize
Number of message IDs currently in the dedup cache.
Sourcepub fn stats(&self) -> GossipStats
pub fn stats(&self) -> GossipStats
Return a snapshot of current statistics.
Sourcepub fn drain_events(&mut self) -> Vec<GossipEvent>
pub fn drain_events(&mut self) -> Vec<GossipEvent>
Drain and return all buffered GossipEvents.
Sourcepub fn config(&self) -> &GossipConfig
pub fn config(&self) -> &GossipConfig
Borrow the current configuration.
Sourcepub fn peer_count(&self) -> usize
pub fn peer_count(&self) -> usize
Number of registered peers (active + inactive).
Sourcepub fn peer(&self, peer_id: &str) -> Option<&GossipPeer>
pub fn peer(&self, peer_id: &str) -> Option<&GossipPeer>
Get a peer by ID.
Sourcepub fn deactivate_peer(&mut self, peer_id: &str) -> Result<(), EngineError>
pub fn deactivate_peer(&mut self, peer_id: &str) -> Result<(), EngineError>
Mark a peer as inactive without removing it.
Sourcepub fn reactivate_peer(&mut self, peer_id: &str) -> Result<(), EngineError>
pub fn reactivate_peer(&mut self, peer_id: &str) -> Result<(), EngineError>
Reactivate a previously deactivated peer.
Sourcepub fn clear_dedup_cache(&mut self)
pub fn clear_dedup_cache(&mut self)
Reset the dedup cache without touching any other state.
Auto Trait Implementations§
impl Freeze for GossipProtocolEngine
impl RefUnwindSafe for GossipProtocolEngine
impl Send for GossipProtocolEngine
impl Sync for GossipProtocolEngine
impl Unpin for GossipProtocolEngine
impl UnsafeUnpin for GossipProtocolEngine
impl UnwindSafe for GossipProtocolEngine
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more