Skip to main content

KeepaliveManager

Struct KeepaliveManager 

Source
pub struct KeepaliveManager { /* private fields */ }
Expand description

Manages keepalive timing, miss counting, and adaptive interval adjustment.

Implementations§

Source§

impl KeepaliveManager

Source

pub fn new(config: KeepaliveConfig) -> Self

Create a new keepalive manager with the given config.

Source

pub fn from_preset(preset: KeepalivePreset) -> Self

Create with a preset.

Source

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;
}
Source

pub fn should_send_keepalive(&self) -> bool

Returns true if a keepalive ping should be sent now.

Source

pub fn record_keepalive_sent(&mut self)

Record that a keepalive ping was sent.

Source

pub fn record_pong_received(&mut self)

Record that a pong was received.

Resets miss counter, updates RTT estimate, adjusts adaptive interval.

Source

pub fn record_pong_missed(&mut self)

Record a missed pong (called after KeepaliveAction::PongTimeout).

Source

pub fn record_activity(&mut self)

Record any activity (data received) — resets the keepalive timer.

Source

pub fn reset_misses(&mut self)

Reset miss counter (e.g. after reconnect).

Source

pub fn srtt(&self) -> Option<Duration>

Returns the current smoothed RTT from keepalive pings.

Source

pub fn current_interval(&self) -> Duration

Returns the current effective keepalive interval (adaptive or configured).

Source

pub fn missed_pongs(&self) -> u32

Returns the number of consecutive missed pongs.

Source

pub fn is_waiting_for_pong(&self) -> bool

Returns true if we are waiting for a pong right now.

Source

pub fn is_dead(&self) -> bool

Returns true if the connection is considered dead (too many missed pongs).

Source

pub fn total_sent(&self) -> u64

Returns total keepalives sent.

Source

pub fn total_pongs(&self) -> u64

Returns total pongs received.

Source

pub fn last_pong(&self) -> Option<Instant>

Returns when the last pong was received.

Source

pub fn config(&self) -> &KeepaliveConfig

Returns a reference to the config.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more