From 41f32a28d977682e4166cc499fb578cbe4303226 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fabian=20Gr=C3=BCnbichler?= Date: Tue, 2 Feb 2021 12:53:09 +0100 Subject: [PATCH] accept/auth: count timeout over whole loop MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit instead of just poll. the manual tracking of elapsed across loop iterations is to avoid underflows - can be replaced with a saturating_sub call once that becomes stable. Signed-off-by: Fabian Grünbichler --- src/main.rs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 21bd066..e85d518 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,12 +96,12 @@ fn read_ticket_line( let mut poll = Poll::new()?; poll.registry().register(stream, Token(0), Interest::READABLE)?; let mut events = Events::with_capacity(1); - let mut timeout = timeout; + + let now = Instant::now(); + let mut elapsed = Duration::new(0, 0); loop { - let now = Instant::now(); - poll.poll(&mut events, Some(timeout))?; - let elapsed = now.elapsed(); + poll.poll(&mut events, Some(timeout - elapsed))?; if !events.is_empty() { match buf.read_from(stream) { Ok(n) => { @@ -122,9 +122,8 @@ fn read_ticket_line( } } - if timeout >= elapsed { - timeout -= elapsed; - } else { + elapsed = now.elapsed(); + if elapsed > timeout { io_bail!("timed out"); } } @@ -207,19 +206,19 @@ fn listen_and_accept( let mut events = Events::with_capacity(1); - let mut timeout = timeout; + let now = Instant::now(); + let mut elapsed = Duration::new(0, 0); + loop { - let now = Instant::now(); - poll.poll(&mut events, Some(timeout))?; - let elapsed = now.elapsed(); + poll.poll(&mut events, Some(timeout - elapsed))?; if !events.is_empty() { let (stream, client) = listener.accept()?; println!("client connection: {:?}", client); return Ok((stream, port)); } - if timeout >= elapsed { - timeout -= elapsed; - } else { + + elapsed = now.elapsed(); + if elapsed > timeout { io_bail!("timed out"); } } -- 2.39.2