pub struct PingTracker { /* private fields */ }
Available on crate feature
ping
only.Expand description
A simple type to track PINGs and if you should PONG
This requires the ping
feature to be enabled
If either sync
or parking_lot
features are also enabled, then this type is safe to send to other threads
use twitch_message::PingTracker;
// create a new tracker, the `threshold` is used to determine when a connection is dead/stale.
let pt = PingTracker::new(std::time::Duration::from_secs(10 * 60));
// in some loop
// if its been a while (such as if you have a way to keep track of time)
if pt.probably_timed_out() {
// we should reconnect
return Err("timed out".into());
}
// this might block for a while
let msg = read_message();
// update the tracker
pt.update(&msg);
// check to see if you should reply.
// this returns a message you can write to your sink
if let Some(pong) = pt.should_pong() {
io_sink.encode_msg(pong)?;
}
Implementations§
Source§impl PingTracker
impl PingTracker
Sourcepub const fn new(threshold: Duration) -> Self
pub const fn new(threshold: Duration) -> Self
Create a new PingTracker
with a ‘timeout’ duration
Sourcepub fn should_pong(&self) -> Option<Pong<'static>>
pub fn should_pong(&self) -> Option<Pong<'static>>
Determines whether you should PONG
This returns the message you should encode
Sourcepub fn probably_timed_out(&self) -> bool
pub fn probably_timed_out(&self) -> bool
Determines if the timeout threshold has been reached
Auto Trait Implementations§
impl !Freeze for PingTracker
impl !RefUnwindSafe for PingTracker
impl Send for PingTracker
impl Sync for PingTracker
impl Unpin for PingTracker
impl UnwindSafe for PingTracker
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