pub struct ElectionManager {
pub node_id: String,
pub term: u64,
pub state: ElectionState,
/* private fields */
}Expand description
Manager for a single-node’s view of the cluster election.
Fields§
§node_id: StringThis node’s identifier.
term: u64Current election term.
state: ElectionStateState of this node in the current term.
Implementations§
Source§impl ElectionManager
impl ElectionManager
Sourcepub fn new(
node_id: impl Into<String>,
cluster_size: usize,
election_timeout: Duration,
) -> Self
pub fn new( node_id: impl Into<String>, cluster_size: usize, election_timeout: Duration, ) -> Self
Create a new election manager for node_id in a cluster of cluster_size nodes.
Sourcepub fn start_election(&mut self)
pub fn start_election(&mut self)
Advance to the next term and transition this node to Candidate.
Sourcepub fn record_vote(&mut self, vote: NodeVote) -> bool
pub fn record_vote(&mut self, vote: NodeVote) -> bool
Record a vote for the current term.
Returns true if the vote was accepted (valid for current term and not a
duplicate from the same voter).
Sourcepub fn winner(&self) -> Option<&str>
pub fn winner(&self) -> Option<&str>
Return the candidate with the most votes, or None if no votes yet.
Sourcepub fn vote_count(&self) -> usize
pub fn vote_count(&self) -> usize
Number of votes received in the current term.
Sourcepub fn is_timed_out(&self, now: Instant) -> bool
pub fn is_timed_out(&self, now: Instant) -> bool
Return true if the election has timed out.
Sourcepub fn become_follower(&mut self)
pub fn become_follower(&mut self)
Transition this node to follower of leader_id, resetting election state.
Sourcepub fn become_leader(&mut self)
pub fn become_leader(&mut self)
Force-set this node as leader (called after winning election externally).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ElectionManager
impl RefUnwindSafe for ElectionManager
impl Send for ElectionManager
impl Sync for ElectionManager
impl Unpin for ElectionManager
impl UnsafeUnpin for ElectionManager
impl UnwindSafe for ElectionManager
Blanket Implementations§
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
Mutably borrows from an owned value. Read more
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
Wrap the input message
T in a tonic::Request