Radish alpha
r
rad:zwTxygwuz5LDGBq255RA2CbNGrz8
Radicle CI broker
Radicle
Git
fix: make RealtimeLines::line honor the max duration setting
Lars Wirzenius committed 8 months ago
commit ac9261bb8e669cee9562e29da44a7e0308dcc490
parent 04cc070
1 file changed +19 -6
modified src/timeoutcmd.rs
@@ -100,7 +100,7 @@ impl TimeoutCommand {
        Self {
            max_duration,
            stdin_data: vec![],
-
            stdout: RealtimeLines::default(),
+
            stdout: RealtimeLines::new(max_duration),
        }
    }

@@ -357,15 +357,25 @@ impl ChildProcess {
/// The buffer can be cloned and each clone is logically the same
/// buffer. This allows separate producer and consumer threads to own
/// the buffer, but internally it's the same one.
-
#[derive(Default, Clone)]
+
#[derive(Clone)]
pub struct RealtimeLines {
    // We have an unlocked buffer protected by a mutex, and a
    // condition variable to signal consumers of new data or the
    // producer having finished.
    data: Arc<(Mutex<UnlockedBuf>, Condvar)>,
+
    started: Instant,
+
    max_duration: Duration,
}

impl RealtimeLines {
+
    fn new(max_duration: Duration) -> Self {
+
        Self {
+
            data: Arc::new((Mutex::new(UnlockedBuf::default()), Condvar::default())),
+
            started: Instant::now(),
+
            max_duration,
+
        }
+
    }
+

    /// Push some binary data to the buffer.
    ///
    /// The incoming data is a byte vector, as we may not receive
@@ -409,10 +419,8 @@ impl RealtimeLines {

        loop {
            match buf.line() {
-
                // We got a line: return it.
-
                Some(line) => {
-
                    return Some(line);
-
                }
+
                // We've been reading for too long.
+
                _ if self.started.elapsed() > self.max_duration => return None,

                // We didn't get a line, but the input stream has
                // finished. Return final partial line, if there is one,
@@ -428,6 +436,11 @@ impl RealtimeLines {
                None => {
                    buf = var.wait(buf).expect("wait for line");
                }
+

+
                // We got a line: return it.
+
                Some(line) => {
+
                    return Some(line);
+
                }
            }
        }
    }