pub struct KeepaliveManager { /* private fields */ }Expand description
Manages keepalive timing, miss counting, and adaptive interval adjustment.
Implementations§
Source§impl KeepaliveManager
impl KeepaliveManager
Sourcepub fn new(config: KeepaliveConfig) -> Self
pub fn new(config: KeepaliveConfig) -> Self
Create a new keepalive manager with the given config.
Sourcepub fn from_preset(preset: KeepalivePreset) -> Self
pub fn from_preset(preset: KeepalivePreset) -> Self
Create with a preset.
Sourcepub fn check(&mut self) -> KeepaliveAction
pub fn check(&mut self) -> KeepaliveAction
Check what keepalive action to take right now.
Call this periodically (e.g. every second) in your main loop.
loop {
match manager.check() {
KeepaliveAction::SendPing => conn.ping().await?,
KeepaliveAction::PongTimeout => manager.record_pong_missed(),
KeepaliveAction::ConnectionDead => break, // reconnect
KeepaliveAction::Idle => {}
}
tokio::time::sleep(Duration::from_secs(1)).await;
}Sourcepub fn should_send_keepalive(&self) -> bool
pub fn should_send_keepalive(&self) -> bool
Returns true if a keepalive ping should be sent now.
Sourcepub fn record_keepalive_sent(&mut self)
pub fn record_keepalive_sent(&mut self)
Record that a keepalive ping was sent.
Sourcepub fn record_pong_received(&mut self)
pub fn record_pong_received(&mut self)
Record that a pong was received.
Resets miss counter, updates RTT estimate, adjusts adaptive interval.
Sourcepub fn record_pong_missed(&mut self)
pub fn record_pong_missed(&mut self)
Record a missed pong (called after KeepaliveAction::PongTimeout).
Sourcepub fn record_activity(&mut self)
pub fn record_activity(&mut self)
Record any activity (data received) — resets the keepalive timer.
Sourcepub fn reset_misses(&mut self)
pub fn reset_misses(&mut self)
Reset miss counter (e.g. after reconnect).
Sourcepub fn current_interval(&self) -> Duration
pub fn current_interval(&self) -> Duration
Returns the current effective keepalive interval (adaptive or configured).
Sourcepub fn missed_pongs(&self) -> u32
pub fn missed_pongs(&self) -> u32
Returns the number of consecutive missed pongs.
Sourcepub fn is_waiting_for_pong(&self) -> bool
pub fn is_waiting_for_pong(&self) -> bool
Returns true if we are waiting for a pong right now.
Sourcepub fn is_dead(&self) -> bool
pub fn is_dead(&self) -> bool
Returns true if the connection is considered dead (too many missed pongs).
Sourcepub fn total_sent(&self) -> u64
pub fn total_sent(&self) -> u64
Returns total keepalives sent.
Sourcepub fn total_pongs(&self) -> u64
pub fn total_pongs(&self) -> u64
Returns total pongs received.
Sourcepub fn config(&self) -> &KeepaliveConfig
pub fn config(&self) -> &KeepaliveConfig
Returns a reference to the config.