public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH xtermjs 3/3] termproxy: rewrite read_ticket_line
Date: Mon,  1 Feb 2021 08:55:18 +0100	[thread overview]
Message-ID: <20210201075518.21727-4-d.csapak@proxmox.com> (raw)
In-Reply-To: <20210201075518.21727-1-d.csapak@proxmox.com>

since we cannot accept a std TcpStream from a mio::net::TcpListener
anymore, we cannot use set_read_timeout here

instead implement the readloop as a mio poll loop similar
to listen_and_accept, otherwise termproxy will busy loop and
consume 100% of a single core during authentication

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/main.rs | 43 ++++++++++++++++++++++++++++++++-----------
 1 file changed, 32 insertions(+), 11 deletions(-)

diff --git a/src/main.rs b/src/main.rs
index 07fa1a8..21bd066 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -92,19 +92,40 @@ fn read_ticket_line(
     buf: &mut ByteBuffer,
     timeout: Duration,
 ) -> TicketResult {
-    let now = Instant::now();
-    while !&buf[..].contains(&b'\n') {
-        if buf.is_full() || now.elapsed() >= timeout {
-            io_bail!("authentication data is incomplete: {:?}", &buf[..]);
-        }
-        match buf.read_from(stream) {
-            Ok(n) => {
-                if n == 0 {
-                    io_bail!("connection closed before authentication");
+
+    let mut poll = Poll::new()?;
+    poll.registry().register(stream, Token(0), Interest::READABLE)?;
+    let mut events = Events::with_capacity(1);
+    let mut timeout = timeout;
+
+    loop {
+        let now = Instant::now();
+        poll.poll(&mut events, Some(timeout))?;
+        let elapsed = now.elapsed();
+        if !events.is_empty() {
+            match buf.read_from(stream) {
+                Ok(n) => {
+                    if n == 0 {
+                        io_bail!("connection closed before authentication");
+                    }
                 }
+                Err(err) if err.kind() == ErrorKind::WouldBlock => {}
+                Err(err) => return Err(err),
             }
-            Err(err) if err.kind() == ErrorKind::WouldBlock => {}
-            Err(err) => return Err(err),
+
+            if buf[..].contains(&b'\n') {
+                break;
+            }
+
+            if buf.is_full() {
+                io_bail!("authentication data is incomplete: {:?}", &buf[..]);
+            }
+        }
+
+        if timeout >= elapsed {
+            timeout -= elapsed;
+        } else {
+            io_bail!("timed out");
         }
     }
 
-- 
2.20.1





  parent reply	other threads:[~2021-02-01  7:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-01  7:55 [pbs-devel] [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0) Dominik Csapak
2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 1/3] Cargo.toml: update mio to 0.7 and proxmox to 0.10 Dominik Csapak
2021-02-01  7:55 ` [pbs-devel] [PATCH xtermjs 2/3] update to mio 0.7 Dominik Csapak
2021-02-01  7:55 ` Dominik Csapak [this message]
2021-02-02 12:10 ` [pbs-devel] applied-series: [PATCH xtermjs 0/3] update to mio 0.7 (part of tokio 1.0) Fabian Grünbichler

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210201075518.21727-4-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pbs-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal