pub struct Foca<T, C, RNG, B: BroadcastHandler<T>> { /* private fields */ }Expand description
Foca is the main interaction point of this crate.
It manages the cluster members and executes the SWIM protocol. It’s intended as a low-level guts-exposed safe view into the protocol allowing any kind of Identity and transport to be used.
Most interactions with Foca require the caller to provide a
Runtime type, which is simply a way to turn the result of an
operation inside out (think callbacks, or an out parameter like
void* out). This allows Foca to avoid deciding anything related
to how it interacts with the operating system.
Implementations
sourceimpl<T, C, RNG> Foca<T, C, RNG, NoCustomBroadcast> where
T: Identity,
C: Codec<T>,
RNG: Rng,
impl<T, C, RNG> Foca<T, C, RNG, NoCustomBroadcast> where
T: Identity,
C: Codec<T>,
RNG: Rng,
sourcepub fn new(identity: T, config: Config, rng: RNG, codec: C) -> Self
pub fn new(identity: T, config: Config, rng: RNG, codec: C) -> Self
Create a new Foca instance with custom broadcasts disabled.
This is a simple shortcut for Foca::with_custom_broadcast
using the NoCustomBroadcast type to deny any form of custom
broadcast.
sourceimpl<T, C, RNG, B> Foca<T, C, RNG, B> where
T: Identity,
C: Codec<T>,
RNG: Rng,
B: BroadcastHandler<T>,
impl<T, C, RNG, B> Foca<T, C, RNG, B> where
T: Identity,
C: Codec<T>,
RNG: Rng,
B: BroadcastHandler<T>,
sourcepub fn with_custom_broadcast(
identity: T,
config: Config,
rng: RNG,
codec: C,
broadcast_handler: B
) -> Self
pub fn with_custom_broadcast(
identity: T,
config: Config,
rng: RNG,
codec: C,
broadcast_handler: B
) -> Self
Initialize a new Foca instance.
sourcepub fn reuse_down_identity(&mut self) -> Result<(), Error>
pub fn reuse_down_identity(&mut self) -> Result<(), Error>
Re-enable joining a cluster with the same identity after being declared Down.
This is intended to be use by implementations that decide not to opt-in on auto-rejoining: once Foca detects its Down you’ll only be able to receive messages (which will likely stop after a short while since the cluster things you are down).
Whatever is controlling the running Foca will then have to wait
for at least Config::remove_down_after before attempting a
rejoin. Then you can call this method followed by a
[Foca::announce(T)] to go back to the cluster.
sourcepub fn change_identity(
&mut self,
new_id: T,
runtime: impl Runtime<T>
) -> Result<(), Error>
pub fn change_identity(
&mut self,
new_id: T,
runtime: impl Runtime<T>
) -> Result<(), Error>
Change the current identity.
Foca will declare its previous identity as Down and immediatelly notify the cluster about the changes.
Notice that changing your identity does not guarantee a successful (re)join. After changing it and disseminating the updates Foca will only know it’s actually accepted after receiving a message addressed to it.
Watch for Notification::Active if you want more confidence about
a successful (re)join.
Intended to be used when identities carry metadata that occasionally changes.
sourcepub fn iter_members(&self) -> impl Iterator<Item = &T>
pub fn iter_members(&self) -> impl Iterator<Item = &T>
Iterate over the currently active cluster members.
sourcepub fn num_members(&self) -> usize
pub fn num_members(&self) -> usize
Returns the number of active members in the cluster.
May only be used as a bound for Foca::iter_members if no
Foca method that takes &mut self is called in-between.
sourcepub fn apply_many(
&mut self,
updates: impl Iterator<Item = Member<T>>,
runtime: impl Runtime<T>
) -> Result<(), Error>
pub fn apply_many(
&mut self,
updates: impl Iterator<Item = Member<T>>,
runtime: impl Runtime<T>
) -> Result<(), Error>
Applies cluster updates to this foca instance.
This is for advanced usage. It’s intended as a way to unlock
more elaborate synchronization protocols: implementations may
choose to unify their cluster knowledge (say: a streaming
join protocol or a periodic sync) and use Foca::apply_many
as a way to feed Foca this new (external) knowledge.
sourcepub fn announce(
&mut self,
dst: T,
runtime: impl Runtime<T>
) -> Result<(), Error>
pub fn announce(
&mut self,
dst: T,
runtime: impl Runtime<T>
) -> Result<(), Error>
Attempt to join the cluster dst belongs to.
Sends a Message::Announce to dst. If accepted, we’ll receive
a Message::Feed as reply.
sourcepub fn gossip(&mut self, runtime: impl Runtime<T>) -> Result<(), Error>
pub fn gossip(&mut self, runtime: impl Runtime<T>) -> Result<(), Error>
Disseminate updates/broadcasts to cluster members.
This instructs Foca to pick Config::num_indirect_probes
random active members and send a Message::Gossip containing
cluster updates.
Intended for more complex scenarios where an implementation wants to attempt reducing the time it takes for information to propagate thoroughly.
sourcepub fn broadcast(&mut self, runtime: impl Runtime<T>) -> Result<(), Error>
pub fn broadcast(&mut self, runtime: impl Runtime<T>) -> Result<(), Error>
Only disseminate custom broadcasts to cluster members
This instructs Foca to pick Config::num_indirect_probes
random active members that pass the
BroadcastHandler::should_add_broadcast_data check. It
guarantees custom broadcast dissemination if there are
candidate members available.
No cluster update will be sent with these messages. Intended
to be used in tandem with a non-default
should_add_broadcast_data.
sourcepub fn leave_cluster(self, runtime: impl Runtime<T>) -> Result<(), Error>
pub fn leave_cluster(self, runtime: impl Runtime<T>) -> Result<(), Error>
Leave the cluster by declaring our own identity as down.
If there are active members, we select a few are selected and notify them of our exit so that the cluster learns about it quickly.
This is the cleanest way to terminate a running Foca.
sourcepub fn add_broadcast(&mut self, data: &[u8]) -> Result<(), Error>
pub fn add_broadcast(&mut self, data: &[u8]) -> Result<(), Error>
Register some data to be broadcast along with Foca messages.
Calls into this instance’s BroadcastHandler and reacts accordingly.
sourcepub fn handle_timer(
&mut self,
event: Timer<T>,
runtime: impl Runtime<T>
) -> Result<(), Error>
pub fn handle_timer(
&mut self,
event: Timer<T>,
runtime: impl Runtime<T>
) -> Result<(), Error>
React to a previously scheduled timer event.
sourcepub fn updates_backlog(&self) -> usize
pub fn updates_backlog(&self) -> usize
Reports the current length of the cluster updates queue.
Updates are transmitted Config::max_transmissions times
at most or until we learn new information about the same
member.
sourcepub fn custom_broadcast_backlog(&self) -> usize
pub fn custom_broadcast_backlog(&self) -> usize
Repports the current length of the custom broadcast queue.
Custom broadcasts are transmitted Config::max_transmissions
times at most or until they get invalidated by another custom
broadcast.
sourcepub fn set_config(&mut self, config: Config) -> Result<(), Error>
pub fn set_config(&mut self, config: Config) -> Result<(), Error>
Replaces the current configuration with a new one.
Most of the time a static configuration is more than enough, but for use-cases where the cluster size can drastically change during normal operations, changing the configuration parameters is a nicer alternative to recreating the Foca instance.
Presently, attempting to change Config::probe_period or
Config::probe_rtt results in Error::InvalidConfig; For
such cases it’s recommended to recreate your Foca instance. When
an error occurrs, every configuration parameter remains
unchanged.
sourcepub fn handle_data(
&mut self,
data: &[u8],
runtime: impl Runtime<T>
) -> Result<(), Error>
pub fn handle_data(
&mut self,
data: &[u8],
runtime: impl Runtime<T>
) -> Result<(), Error>
Handle data received from the network.
Data larger than the configured limit will be rejected. Errors are expected if you’re receiving arbitrary data (which very likely if you are listening to a socket address).
Trait Implementations
Auto Trait Implementations
impl<T, C, RNG, B> RefUnwindSafe for Foca<T, C, RNG, B> where
B: RefUnwindSafe,
C: RefUnwindSafe,
RNG: RefUnwindSafe,
T: RefUnwindSafe,
<B as BroadcastHandler<T>>::Broadcast: RefUnwindSafe,
impl<T, C, RNG, B> Send for Foca<T, C, RNG, B> where
B: Send,
C: Send,
RNG: Send,
T: Send,
<B as BroadcastHandler<T>>::Broadcast: Send,
impl<T, C, RNG, B> Sync for Foca<T, C, RNG, B> where
B: Sync,
C: Sync,
RNG: Sync,
T: Sync,
<B as BroadcastHandler<T>>::Broadcast: Sync,
impl<T, C, RNG, B> Unpin for Foca<T, C, RNG, B> where
B: Unpin,
C: Unpin,
RNG: Unpin,
T: Unpin,
<B as BroadcastHandler<T>>::Broadcast: Unpin,
impl<T, C, RNG, B> UnwindSafe for Foca<T, C, RNG, B> where
B: UnwindSafe,
C: UnwindSafe,
RNG: UnwindSafe,
T: UnwindSafe,
<B as BroadcastHandler<T>>::Broadcast: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber to this type, returning a
WithDispatch wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more