]> git.proxmox.com Git - pve-xtermjs.git/commitdiff
accept/auth: count timeout over whole loop
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Tue, 2 Feb 2021 11:53:09 +0000 (12:53 +0100)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Tue, 2 Feb 2021 12:08:15 +0000 (13:08 +0100)
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 <f.gruenbichler@proxmox.com>
src/main.rs

index 21bd066d1a8cddb5cd824406e76062f72cb6a245..e85d518c1c09bfa5b2464f96f9b4a734b582b350 100644 (file)
@@ -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");
         }
     }