From ec774745113e9aa87a0dceaf30922957eef4e28d Mon Sep 17 00:00:00 2001 From: Benjamin Saunders Date: Sun, 16 Dec 2018 21:04:31 -0800 Subject: [PATCH] Tweak timer I/O representation --- quinn-proto/examples/connect.rs | 28 +++++++++++------- quinn-proto/src/connection.rs | 3 ++ quinn-proto/src/endpoint.rs | 29 ++++++------------- quinn-proto/src/lib.rs | 2 +- quinn-proto/src/tests.rs | 51 +++++++++++++++------------------ quinn/src/lib.rs | 14 +++++---- 6 files changed, 61 insertions(+), 66 deletions(-) diff --git a/quinn-proto/examples/connect.rs b/quinn-proto/examples/connect.rs index 6a245b84b..8ab395204 100644 --- a/quinn-proto/examples/connect.rs +++ b/quinn-proto/examples/connect.rs @@ -9,7 +9,9 @@ use std::sync::Arc; use std::time::{Duration, Instant}; use failure::Error; -use quinn_proto::{self as quinn, Config, Directionality, Endpoint, Event, Io, ReadError, Timer}; +use quinn_proto::{ + self as quinn, Config, Directionality, Endpoint, Event, Io, ReadError, Timer, TimerUpdate, +}; use rustls::ProtocolVersion; use slog::{Drain, Logger}; @@ -159,41 +161,45 @@ impl Context { sent += 1; self.socket.send_to(&packet, destination)?; } - Io::TimerStart { + Io::TimerUpdate { timer: Timer::LossDetection, - time, + update: TimerUpdate::Start(time), .. } => { self.loss_timer = Some(time); } - Io::TimerStart { + Io::TimerUpdate { timer: Timer::Close, - time, + update: TimerUpdate::Start(time), .. } => { self.close_timer = Some(time); } - Io::TimerStart { + Io::TimerUpdate { timer: Timer::Idle, - time, + update: TimerUpdate::Start(time), .. } => { self.idle_timer = Some(time); } - Io::TimerStop { + Io::TimerUpdate { timer: Timer::LossDetection, + update: TimerUpdate::Stop, .. } => { self.loss_timer = None; } - Io::TimerStop { + Io::TimerUpdate { timer: Timer::Close, + update: TimerUpdate::Stop, .. } => { self.close_timer = None; } - Io::TimerStop { - timer: Timer::Idle, .. + Io::TimerUpdate { + timer: Timer::Idle, + update: TimerUpdate::Stop, + .. } => unreachable!(), } } diff --git a/quinn-proto/src/connection.rs b/quinn-proto/src/connection.rs index 358b8ccab..0ff077fd4 100644 --- a/quinn-proto/src/connection.rs +++ b/quinn-proto/src/connection.rs @@ -2792,8 +2792,11 @@ impl IoQueue { } } +/// Changes to a connection's timers #[derive(Debug, Copy, Clone)] pub enum TimerUpdate { + /// Set the timer to expire at an a certain point in time, in absolute microseconds Start(u64), + /// Cancel time timer if it's currently running Stop, } diff --git a/quinn-proto/src/endpoint.rs b/quinn-proto/src/endpoint.rs index 6d176bc2c..dbb550791 100644 --- a/quinn-proto/src/endpoint.rs +++ b/quinn-proto/src/endpoint.rs @@ -15,6 +15,7 @@ use slog::{self, Logger}; use crate::coding::BufMutExt; use crate::connection::{ self, handshake_close, ClientConfig, Connection, ConnectionError, ConnectionHandle, State, + TimerUpdate, }; use crate::crypto::{self, reset_token_for, ConnectError, Crypto, TlsSession, TokenKey}; use crate::packet::{ @@ -288,20 +289,10 @@ impl Endpoint { ecn, packet, }, - connection::Io::TimerUpdate { - timer, - update: connection::TimerUpdate::Stop, - } => Io::TimerStop { + connection::Io::TimerUpdate { timer, update } => Io::TimerUpdate { connection: conn, timer, - }, - connection::Io::TimerUpdate { - timer, - update: connection::TimerUpdate::Start(time), - } => Io::TimerStart { - connection: conn, - timer, - time, + update, }, }); } else { @@ -733,9 +724,10 @@ impl Endpoint { pub fn timeout(&mut self, now: u64, conn: ConnectionHandle, timer: Timer) { match timer { Timer::Close => { - self.ctx.io.push_back(Io::TimerStop { + self.ctx.io.push_back(Io::TimerUpdate { connection: conn, timer: Timer::Idle, + update: TimerUpdate::Stop, }); self.ctx.events.push_back((conn, Event::ConnectionDrained)); if self.connections[conn.0].app_closed { @@ -947,16 +939,11 @@ pub enum Io { ecn: Option, packet: Box<[u8]>, }, - /// Start or reset a timer - TimerStart { - connection: ConnectionHandle, - timer: Timer, - /// Absolute μs - time: u64, - }, - TimerStop { + /// Start, stop, or reset a timer + TimerUpdate { connection: ConnectionHandle, timer: Timer, + update: TimerUpdate, }, } diff --git a/quinn-proto/src/lib.rs b/quinn-proto/src/lib.rs index 1cda33fc0..911d85b2c 100644 --- a/quinn-proto/src/lib.rs +++ b/quinn-proto/src/lib.rs @@ -24,7 +24,7 @@ mod transport_parameters; mod varint; mod connection; -pub use crate::connection::{ConnectionError, ConnectionHandle}; +pub use crate::connection::{ConnectionError, ConnectionHandle, TimerUpdate}; mod crypto; pub use crate::crypto::{ClientConfig, ConnectError, TokenKey}; diff --git a/quinn-proto/src/tests.rs b/quinn-proto/src/tests.rs index 3ef91b9b2..0f647b306 100644 --- a/quinn-proto/src/tests.rs +++ b/quinn-proto/src/tests.rs @@ -313,19 +313,33 @@ impl TestEndpoint { Io::Transmit { packet, ecn, .. } => { self.outbound.push_back((ecn, packet)); } - Io::TimerStart { + Io::TimerUpdate { timer, - time, + update, connection, } => { self.conn = Some(connection); - trace!( - log, - "{side:?} {timer:?} start: {dt}", - side = self.side, - timer = timer, - dt = (time - now) - ); + let time = match update { + TimerUpdate::Stop => { + trace!( + log, + "{side:?} {timer:?} stop", + side = self.side, + timer = timer + ); + u64::max_value() + } + TimerUpdate::Start(time) => { + trace!( + log, + "{side:?} {timer:?} start: {dt}", + side = self.side, + timer = timer, + dt = (time - now) + ); + time + } + }; match timer { Timer::LossDetection => { self.loss = time; @@ -338,25 +352,6 @@ impl TestEndpoint { } } } - Io::TimerStop { timer, .. } => { - trace!( - log, - "{side:?} {timer:?} stop", - side = self.side, - timer = timer - ); - match timer { - Timer::LossDetection => { - self.loss = u64::max_value(); - } - Timer::Idle => { - self.idle = u64::max_value(); - } - Timer::Close => { - self.close = u64::max_value(); - } - } - } } } } diff --git a/quinn/src/lib.rs b/quinn/src/lib.rs index 1cd8c8a93..2822e934a 100644 --- a/quinn/src/lib.rs +++ b/quinn/src/lib.rs @@ -802,10 +802,10 @@ impl Future for Driver { endpoint.outgoing.push_front((destination, packet)); } } - TimerStart { + TimerUpdate { connection, timer: timer @ quinn::Timer::Close, - time, + update: quinn::TimerUpdate::Start(time), } => { let instant = endpoint.epoch + duration_micros(time); endpoint.timers.push(Timer { @@ -815,10 +815,10 @@ impl Future for Driver { cancel: None, }); } - TimerStart { + TimerUpdate { connection, timer, - time, + update: quinn::TimerUpdate::Start(time), } => { // Loss detection and idle timers start before the connection is established let pending = endpoint @@ -845,7 +845,11 @@ impl Future for Driver { cancel: Some(recv), }); } - TimerStop { connection, timer } => { + TimerUpdate { + connection, + timer, + update: quinn::TimerUpdate::Stop, + } => { trace!(endpoint.log, "timer stop"; "timer" => ?timer); // If a connection was lost, we already canceled its loss/idle timers. if let Some(pending) = endpoint.pending.get_mut(&connection) {