pub struct LeaderElectionManager {
pub state: Rc<RefCell<LeaderElectionState>>,
pub heartbeat_interval: Option<i32>,
/* private fields */
}Expand description
Manager for multi-tab leader election
Fields§
§state: Rc<RefCell<LeaderElectionState>>§heartbeat_interval: Option<i32>Implementations§
Source§impl LeaderElectionManager
impl LeaderElectionManager
Sourcepub fn new(db_name: String) -> Self
pub fn new(db_name: String) -> Self
Create new leader election manager with deterministic instance ID
Sourcepub async fn start_election(&mut self) -> Result<(), DatabaseError>
pub async fn start_election(&mut self) -> Result<(), DatabaseError>
Start leader election process using localStorage coordination
Sourcepub async fn try_become_leader_internal(
&mut self,
force: bool,
) -> Result<(), DatabaseError>
pub async fn try_become_leader_internal( &mut self, force: bool, ) -> Result<(), DatabaseError>
Try to become leader using localStorage-based atomic coordination
§Arguments
force- If true, ignores existing leader’s valid lease and forces takeover
Sourcepub async fn try_become_leader(&mut self) -> Result<(), DatabaseError>
pub async fn try_become_leader(&mut self) -> Result<(), DatabaseError>
Try to become leader (respects existing leader’s lease)
Sourcepub async fn force_become_leader(&mut self) -> Result<(), DatabaseError>
pub async fn force_become_leader(&mut self) -> Result<(), DatabaseError>
Force leadership takeover (ignores existing leader’s lease)
Sourcepub fn start_heartbeat(&mut self) -> Result<(), DatabaseError>
pub fn start_heartbeat(&mut self) -> Result<(), DatabaseError>
Start sending heartbeats as leader using localStorage
Sourcepub async fn stop_election(&mut self) -> Result<(), DatabaseError>
pub async fn stop_election(&mut self) -> Result<(), DatabaseError>
Stop leader election (e.g., when tab is closing)
Sourcepub async fn is_leader(&self) -> bool
pub async fn is_leader(&self) -> bool
Check if this instance is the leader (with localStorage validation and re-election)
Sourcepub async fn send_heartbeat(&self) -> Result<(), DatabaseError>
pub async fn send_heartbeat(&self) -> Result<(), DatabaseError>
Send a heartbeat (for testing)
Sourcepub async fn get_last_heartbeat(&self) -> u64
pub async fn get_last_heartbeat(&self) -> u64
Get timestamp of last received leader heartbeat from localStorage
Trait Implementations§
Source§impl Drop for LeaderElectionManager
Drop implementation to prevent “closure invoked after being dropped” errors
impl Drop for LeaderElectionManager
Drop implementation to prevent “closure invoked after being dropped” errors
CRITICAL: Invalidate heartbeat_valid BEFORE clearing interval. The heartbeat closure is intentionally leaked via forget() so it’s never dropped. Any pending setInterval ticks will safely invoke the leaked closure which immediately returns due to the validity check.