diff --git a/src/onion/circuit.rs b/src/onion/circuit.rs index c0dca91..7fd5c24 100644 --- a/src/onion/circuit.rs +++ b/src/onion/circuit.rs @@ -21,9 +21,9 @@ use tokio::time; use tokio::time::Duration; /// timeout applied if there is no traffic on a circuit -pub(crate) const TIMEOUT_IDLE: u64 = 120; +pub(crate) const IDLE_TIMEOUT: Duration = Duration::from_secs(120); /// timeout applied for a teardown operation -const TIMEOUT_TEARDOWN: u64 = 5; +const TEARDOWN_TIMEOUT: Duration = Duration::from_secs(5); pub(crate) type CircuitId = u16; @@ -51,7 +51,7 @@ impl Circuit { } pub(crate) async fn teardown_with_timeout(&self, rng: &rand::SystemRandom) { - match time::timeout(Duration::from_secs(TIMEOUT_TEARDOWN), { + match time::timeout(TEARDOWN_TIMEOUT, { // NOTE: Ignore any errors self.socket().await.teardown(self.id, rng) }) @@ -148,10 +148,7 @@ impl CircuitHandler { }) } else { trace!("Incoming handshake failed post-handshake: unable to derive key"); - let _ = time::timeout(Duration::from_secs(TIMEOUT_TEARDOWN), { - socket.teardown(circuit_id, &rng) - }) - .await; + let _ = time::timeout(TEARDOWN_TIMEOUT, socket.teardown(circuit_id, &rng)).await; Err(anyhow!( "Incoming handshake failed post-handshake: unable to derive key" )) @@ -174,7 +171,7 @@ impl CircuitHandler { async fn respond_loop(&mut self) -> Result<()> { loop { - let mut delay = time::delay_for(Duration::from_secs(TIMEOUT_IDLE)); + let mut delay = time::delay_for(IDLE_TIMEOUT); match &mut self.state { State::Default => { diff --git a/src/onion/socket.rs b/src/onion/socket.rs index ba399d9..f5fbdd6 100644 --- a/src/onion/socket.rs +++ b/src/onion/socket.rs @@ -11,10 +11,10 @@ use tokio::net::TcpStream; use tokio::prelude::*; use tokio::time::{timeout, Duration, Elapsed}; -/// timeout apllied during a read on the socket -const TIMEOUT_READ: u64 = 5; -/// timeout apllied during a write on the socket -const TIMEOUT_WRITE: u64 = 2; +/// timeout applied during a read on the socket +const READ_TIMEOUT: Duration = Duration::from_secs(5); +/// timeout applied during a write on the socket +const WRITE_TIMEOUT: Duration = Duration::from_secs(2); #[derive(Error, Debug)] pub(crate) enum OnionSocketError { @@ -90,11 +90,7 @@ impl OnionSocket { impl OnionSocket { async fn read_buf_from_stream(&mut self) -> SocketResult { - Ok(timeout( - Duration::from_secs(TIMEOUT_READ), - self.stream.read_exact(&mut self.buf), - ) - .await??) + Ok(timeout(READ_TIMEOUT, self.stream.read_exact(&mut self.buf)).await??) } /// Listends for incoming `CIRCUIT CREATE` messages and returns the circuit id and key in this @@ -137,11 +133,7 @@ impl OnionSocket { impl OnionSocket { async fn write_buf_to_stream(&mut self) -> SocketResult<()> { - Ok(timeout( - Duration::from_secs(TIMEOUT_WRITE), - self.stream.write_all(self.buf.as_ref()), - ) - .await??) + Ok(timeout(WRITE_TIMEOUT, self.stream.write_all(self.buf.as_ref())).await??) } async fn encrypt_and_send_opaque( diff --git a/src/onion/tests.rs b/src/onion/tests.rs index a9b5b82..154af57 100644 --- a/src/onion/tests.rs +++ b/src/onion/tests.rs @@ -9,8 +9,7 @@ use tokio::stream; const TEST_IP: IpAddr = IpAddr::V4(Ipv4Addr::LOCALHOST); static PORT_COUNTER: AtomicU16 = AtomicU16::new(42000); -const TIME_ERROR_TIMEOUT: u64 = 4; -const ERROR_TIMEOUT: Duration = Duration::from_secs(TIME_ERROR_TIMEOUT); +const ERROR_TIMEOUT: Duration = Duration::from_secs(4); pub(crate) fn read_rsa_keypair>(path: P) -> Result<(RsaPrivateKey, RsaPublicKey)> { let private_key = RsaPrivateKey::from_pem_file(path)?; @@ -269,9 +268,7 @@ async fn test_timeout() -> Result<()> { Err(_) => panic!("Expected ready event, got timeout"), } - let mut delay = time::delay_for(Duration::from_secs( - circuit::TIMEOUT_IDLE + TIME_ERROR_TIMEOUT, - )); + let mut delay = time::delay_for(circuit::IDLE_TIMEOUT + ERROR_TIMEOUT); tokio::select! { _ = handler_task => Ok(()), _ = &mut delay => {