-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract pingtracker and use it on the relay server
- Loading branch information
1 parent
9d1917b
commit 8cf7dc5
Showing
4 changed files
with
92 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::time::{Duration, Instant}; | ||
|
||
use tracing::debug; | ||
|
||
/// Tracks pings on a single relay connection. | ||
/// | ||
/// Only the last ping needs is useful, any previously sent ping is forgotten and ignored. | ||
#[derive(Debug)] | ||
pub struct PingTracker { | ||
inner: Option<PingInner>, | ||
default_timeout: Duration, | ||
} | ||
|
||
#[derive(Debug)] | ||
struct PingInner { | ||
data: [u8; 8], | ||
deadline: Instant, | ||
} | ||
|
||
impl PingTracker { | ||
/// Creates a new ping tracker, setting the ping timeout for pings. | ||
pub fn new(default_timeout: Duration) -> Self { | ||
Self { | ||
inner: None, | ||
default_timeout, | ||
} | ||
} | ||
|
||
/// Starts a new ping. | ||
pub fn new_ping(&mut self) -> [u8; 8] { | ||
let ping_data = rand::random(); | ||
debug!(data = ?ping_data, "Sending ping to relay server."); | ||
self.inner = Some(PingInner { | ||
data: ping_data, | ||
deadline: Instant::now() + self.default_timeout, | ||
}); | ||
ping_data | ||
} | ||
|
||
/// Updates the ping tracker with a received pong. | ||
/// | ||
/// Only the pong of the most recent ping will do anything. There is no harm feeding | ||
/// any pong however. | ||
pub fn pong_received(&mut self, data: [u8; 8]) { | ||
if self.inner.as_ref().map(|inner| inner.data) == Some(data) { | ||
debug!(?data, "Pong received from relay server"); | ||
self.inner = None; | ||
} | ||
} | ||
|
||
/// Cancel-safe waiting for a ping timeout. | ||
/// | ||
/// Unless the most recent sent ping times out, this will never return. | ||
pub async fn timeout(&mut self) { | ||
match self.inner { | ||
Some(PingInner { deadline, data }) => { | ||
tokio::time::sleep_until(deadline.into()).await; | ||
debug!(?data, "Ping timeout."); | ||
self.inner = None; | ||
} | ||
None => std::future::pending().await, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters