fix: reduce TUI idle CPU and improve server error banners
- Replace non-blocking try_recv() event loop with blocking recv() to eliminate idle spin; the sender tick timeout still drives minimum wakeup rate so the UI stays responsive - Lower default tick rate from 50 ms to 100 ms (10 ticks/sec vs 20); add MANAGARR_TICK_RATE_MS env var for runtime tuning - Move terminal draws to event boundaries (key + tick) instead of every loop pass, reducing unnecessary redraws - In network error handling, prefer concise JSON fields (message / errorMessage / error) over the raw HTTP body, so Radarr ASP.NET stack traces don't flood the TUI banner; full body still logged - Add unit tests for tick-rate parsing and Radarr-style JSON error body Observed: ~0.6% total CPU at idle vs measurably higher with prod defaults
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
use anyhow::Result;
|
||||
use std::env;
|
||||
use std::sync::mpsc;
|
||||
use std::sync::mpsc::Receiver;
|
||||
use std::thread;
|
||||
@@ -18,10 +19,24 @@ pub struct Events {
|
||||
rx: Receiver<InputEvent<Key>>,
|
||||
}
|
||||
|
||||
const DEFAULT_TICK_RATE_MS: u64 = 100;
|
||||
|
||||
fn configured_tick_rate_ms_from(raw: Option<&str>) -> u64 {
|
||||
raw
|
||||
.and_then(|value| value.parse::<u64>().ok())
|
||||
.filter(|ms| *ms > 0)
|
||||
.unwrap_or(DEFAULT_TICK_RATE_MS)
|
||||
}
|
||||
|
||||
fn configured_tick_rate_ms() -> u64 {
|
||||
let raw = env::var("MANAGARR_TICK_RATE_MS").ok();
|
||||
configured_tick_rate_ms_from(raw.as_deref())
|
||||
}
|
||||
|
||||
impl Events {
|
||||
pub fn new() -> Self {
|
||||
let (tx, rx) = mpsc::channel();
|
||||
let tick_rate: Duration = Duration::from_millis(50);
|
||||
let tick_rate: Duration = Duration::from_millis(configured_tick_rate_ms());
|
||||
|
||||
thread::spawn(move || {
|
||||
let mut last_tick = Instant::now();
|
||||
@@ -51,9 +66,33 @@ impl Events {
|
||||
}
|
||||
|
||||
pub fn next(&self) -> Result<Option<InputEvent<Key>>> {
|
||||
match self.rx.try_recv() {
|
||||
match self.rx.recv() {
|
||||
Ok(event) => Ok(Some(event)),
|
||||
_ => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn defaults_to_a_slower_idle_tick_rate() {
|
||||
assert_eq!(DEFAULT_TICK_RATE_MS, 100);
|
||||
assert_eq!(configured_tick_rate_ms_from(None), 100);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parses_positive_tick_rates_and_rejects_invalid_values() {
|
||||
assert_eq!(configured_tick_rate_ms_from(Some("250")), 250);
|
||||
assert_eq!(
|
||||
configured_tick_rate_ms_from(Some("0")),
|
||||
DEFAULT_TICK_RATE_MS
|
||||
);
|
||||
assert_eq!(
|
||||
configured_tick_rate_ms_from(Some("abc")),
|
||||
DEFAULT_TICK_RATE_MS
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user