-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(network): support the
turmoil
transport
- Loading branch information
Showing
10 changed files
with
347 additions
and
26 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
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,62 @@ | ||
use std::{ | ||
sync::{ | ||
atomic::{AtomicU32, Ordering}, | ||
Arc, | ||
}, | ||
time::Duration, | ||
}; | ||
|
||
use tokio::time::Instant; | ||
|
||
// === IdleTracker === | ||
|
||
/// Measures the time since the last activity on a socket. | ||
/// It's used to implement health checks based on idle timeout. | ||
pub(crate) struct IdleTracker { | ||
track: IdleTrack, | ||
prev_value: u32, | ||
prev_time: Instant, | ||
} | ||
|
||
impl IdleTracker { | ||
pub(super) fn new() -> (Self, IdleTrack) { | ||
let track = IdleTrack(<_>::default()); | ||
let this = Self { | ||
track: track.clone(), | ||
prev_value: track.get(), | ||
prev_time: Instant::now(), | ||
}; | ||
|
||
(this, track) | ||
} | ||
|
||
/// Returns the elapsed time since the last `check()` call | ||
/// that observed any [`IdleTrack::update()`] calls. | ||
pub(crate) fn check(&mut self) -> Duration { | ||
let now = Instant::now(); | ||
let new_value = self.track.get(); | ||
|
||
if self.prev_value != new_value { | ||
self.prev_value = new_value; | ||
self.prev_time = now; | ||
} | ||
|
||
now.duration_since(self.prev_time) | ||
} | ||
} | ||
|
||
// === IdleTrack === | ||
|
||
#[derive(Clone)] | ||
pub(super) struct IdleTrack(Arc<AtomicU32>); | ||
|
||
impl IdleTrack { | ||
/// Marks this socket as non-idle. | ||
pub(super) fn update(&self) { | ||
self.0.fetch_add(1, Ordering::Relaxed); | ||
} | ||
|
||
fn get(&self) -> u32 { | ||
self.0.load(Ordering::Relaxed) | ||
} | ||
} |
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,73 @@ | ||
use std::net::SocketAddr; | ||
|
||
use derive_more::Display; | ||
use eyre::{Result, WrapErr}; | ||
use futures::Stream; | ||
use tracing::warn; | ||
use turmoil06::net::{TcpListener, TcpStream}; | ||
|
||
pub(super) use turmoil06::net::tcp::{OwnedReadHalf, OwnedWriteHalf}; | ||
|
||
const PORT: u16 = 0xE1F0; | ||
|
||
#[derive(Clone, Display)] | ||
#[display("turmoil06(local={local}, peer={peer})")] // TODO: use `valuable` after tracing#1570 | ||
pub(crate) struct SocketInfo { | ||
local: String, | ||
peer: String, | ||
} | ||
|
||
pub(super) struct Socket { | ||
pub(super) read: OwnedReadHalf, | ||
pub(super) write: OwnedWriteHalf, | ||
pub(super) info: SocketInfo, | ||
} | ||
|
||
fn prepare_stream(stream: TcpStream) -> Result<Socket> { | ||
let info = SocketInfo { | ||
local: stringify_addr(stream.local_addr().wrap_err("cannot get local addr")?), | ||
peer: stringify_addr(stream.peer_addr().wrap_err("cannot get peer addr")?), | ||
}; | ||
|
||
let (read, write) = stream.into_split(); | ||
Ok(Socket { read, write, info }) | ||
} | ||
|
||
fn stringify_addr(addr: SocketAddr) -> String { | ||
let (ip, port) = (addr.ip(), addr.port()); | ||
let host = turmoil06::reverse_lookup(ip).unwrap_or_else(|| ip.to_string()); | ||
format!("{host}:{port}") | ||
} | ||
|
||
pub(super) async fn connect(host: &str) -> Result<Socket> { | ||
prepare_stream(TcpStream::connect((host, PORT)).await?) | ||
} | ||
|
||
pub(super) async fn listen(host: &str) -> Result<impl Stream<Item = Socket> + 'static> { | ||
let listener = TcpListener::bind((host, PORT)).await?; | ||
|
||
let accept = move |listener: TcpListener| async move { | ||
loop { | ||
let result = listener | ||
.accept() | ||
.await | ||
.map_err(Into::into) | ||
.and_then(|(socket, _)| prepare_stream(socket)); | ||
|
||
match result { | ||
Ok(socket) => return Some((socket, listener)), | ||
Err(err) => { | ||
warn!( | ||
message = "cannot accept TCP connection", | ||
error = %err, | ||
// TODO: addr | ||
); | ||
|
||
// Continue listening. | ||
} | ||
} | ||
} | ||
}; | ||
|
||
Ok(futures::stream::unfold(listener, accept)) | ||
} |
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,10 @@ | ||
#![allow(dead_code)] // TODO: combine tests into "it/*" | ||
|
||
// For tests without `elfo::test::proxy`. | ||
pub(crate) fn setup_logger() { | ||
let _ = tracing_subscriber::fmt() | ||
.with_target(false) | ||
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) | ||
.with_test_writer() | ||
.try_init(); | ||
} |
Oops, something went wrong.