Trait foca::BroadcastHandler
source · [−]pub trait BroadcastHandler<T> {
type Broadcast: Invalidates + AsRef<[u8]>;
type Error: Debug + Display + Send + Sync + 'static;
fn receive_item(
&mut self,
data: impl Buf
) -> Result<Option<Self::Broadcast>, Self::Error>;
fn should_add_broadcast_data(&self, _member: &T) -> bool { ... }
}Expand description
A type capable of decoding a (associated) broadcast from a buffer and deciding whether to keep disseminating it for other members of the cluster (when it’s new information) or to discard it (when its outdated/stale).
Required Associated Types
Concrete type that will be disseminated to all cluster members.
It should be able to compare itself against an arbitrary number
of other Self::Broadcast instances and decide wether it
replaces it or not so conflicting/stale information isn’t
disseminated.
The AsRef<[u8]> part is what gets sent over the wire, which
Self::receive_item is supposed to decode.
Required Methods
Decodes a Self::Broadcast from a buffer and either discards
it or tells Foca to persist and disseminate it.
When you receive a broadcast you have to decide whether it’s
new information that needs to be disseminated (Ok(Some(...)))
or not (Ok(None)).
Always yielding Some(...) is wrong because Foca will never
know when to stop sending this information to other members.
Example: Assume your custom broadcast is a simple Set-Key-Value
operation. When you receive it you should check if your map
contains the Key-Value pair; If it didn’t, you yield
Some, otherwise the operation is stale, so you yield None.
Implementations MUST read a single Self::Broadcast from the
buffer and advance the cursor accordingly.
Implementations may assume the data in the buffer is contiguous.
Provided Methods
fn should_add_broadcast_data(&self, _member: &T) -> bool
fn should_add_broadcast_data(&self, _member: &T) -> bool
Decides whether Foca should add broadcast data to the message
it’s about to send to active member T.
Normally when Foca sends a message it always tries to include custom broadcasts alongside the information it actually cares about; This allows implementations to override this logic with something else.
Example: You are running a heterogeneous cluster and some nodes are always very busy and you’d rather they never have to deal with the extra cpu/bandwidth cost of receiving/sending your custom broadcasts.
Returning true tells Foca to proceed as it would normally,
including broadcasts in the messages it sends when it can.
Returning false tells Foca to not include such broadcasts
in the message. It does not prevent the message from being
sent, just keeps Foca from attaching extra data to them.