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 anyhow::Result;
|
||||||
|
use std::env;
|
||||||
use std::sync::mpsc;
|
use std::sync::mpsc;
|
||||||
use std::sync::mpsc::Receiver;
|
use std::sync::mpsc::Receiver;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
@@ -18,10 +19,24 @@ pub struct Events {
|
|||||||
rx: Receiver<InputEvent<Key>>,
|
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 {
|
impl Events {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
let (tx, rx) = mpsc::channel();
|
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 || {
|
thread::spawn(move || {
|
||||||
let mut last_tick = Instant::now();
|
let mut last_tick = Instant::now();
|
||||||
@@ -51,9 +66,33 @@ impl Events {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn next(&self) -> Result<Option<InputEvent<Key>>> {
|
pub fn next(&self) -> Result<Option<InputEvent<Key>>> {
|
||||||
match self.rx.try_recv() {
|
match self.rx.recv() {
|
||||||
Ok(event) => Ok(Some(event)),
|
Ok(event) => Ok(Some(event)),
|
||||||
_ => Ok(None),
|
_ => 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
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+12
-4
@@ -276,22 +276,30 @@ async fn start_ui(
|
|||||||
|
|
||||||
let input_events = Events::new();
|
let input_events = Events::new();
|
||||||
|
|
||||||
loop {
|
{
|
||||||
let mut app = app.lock().await;
|
let mut app = app.lock().await;
|
||||||
|
|
||||||
terminal.draw(|f| ui(f, &mut app))?;
|
terminal.draw(|f| ui(f, &mut app))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
loop {
|
||||||
match input_events.next()? {
|
match input_events.next()? {
|
||||||
Some(InputEvent::KeyEvent(key)) => {
|
Some(InputEvent::KeyEvent(key)) => {
|
||||||
|
let mut app = app.lock().await;
|
||||||
|
|
||||||
if key == Key::Char('q') && !app.ignore_special_keys_for_textbox_input {
|
if key == Key::Char('q') && !app.ignore_special_keys_for_textbox_input {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
handlers::handle_events(key, &mut app);
|
handlers::handle_events(key, &mut app);
|
||||||
|
terminal.draw(|f| ui(f, &mut app))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(InputEvent::Tick) => app.on_tick().await,
|
Some(InputEvent::Tick) => {
|
||||||
_ => {}
|
let mut app = app.lock().await;
|
||||||
|
app.on_tick().await;
|
||||||
|
terminal.draw(|f| ui(f, &mut app))?;
|
||||||
|
}
|
||||||
|
None => break,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+19
-4
@@ -9,6 +9,7 @@ use regex::Regex;
|
|||||||
use reqwest::{Client, RequestBuilder};
|
use reqwest::{Client, RequestBuilder};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde::de::DeserializeOwned;
|
use serde::de::DeserializeOwned;
|
||||||
|
use serde_json::Value;
|
||||||
use sonarr_network::SonarrEvent;
|
use sonarr_network::SonarrEvent;
|
||||||
use strum_macros::Display;
|
use strum_macros::Display;
|
||||||
use tokio::select;
|
use tokio::select;
|
||||||
@@ -142,11 +143,8 @@ impl<'a, 'b> Network<'a, 'b> {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let status = response.status();
|
let status = response.status();
|
||||||
let whitespace_regex = Regex::new(r"\s+")?;
|
|
||||||
let response_body = response.text().await.unwrap_or_default();
|
let response_body = response.text().await.unwrap_or_default();
|
||||||
let error_body = whitespace_regex
|
let error_body = format_error_body(&response_body)?;
|
||||||
.replace_all(&response_body.replace('\n', " "), " ")
|
|
||||||
.to_string();
|
|
||||||
|
|
||||||
error!("Request failed. Received {status} response code with body: {response_body}");
|
error!("Request failed. Received {status} response code with body: {response_body}");
|
||||||
self.app.lock().await.handle_error(anyhow!("Request failed. Received {status} response code with body: {error_body}"));
|
self.app.lock().await.handle_error(anyhow!("Request failed. Received {status} response code with body: {error_body}"));
|
||||||
@@ -279,6 +277,23 @@ impl<'a, 'b> Network<'a, 'b> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn format_error_body(response_body: &str) -> Result<String> {
|
||||||
|
let whitespace_regex = Regex::new(r"\s+")?;
|
||||||
|
let body = serde_json::from_str::<Value>(response_body)
|
||||||
|
.ok()
|
||||||
|
.and_then(|value| {
|
||||||
|
value
|
||||||
|
.get("message")
|
||||||
|
.or_else(|| value.get("errorMessage"))
|
||||||
|
.or_else(|| value.get("error"))
|
||||||
|
.and_then(Value::as_str)
|
||||||
|
.map(ToOwned::to_owned)
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| response_body.replace('\n', " "));
|
||||||
|
|
||||||
|
Ok(whitespace_regex.replace_all(&body, " ").to_string())
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, Display, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, Display, PartialEq, Eq)]
|
||||||
pub enum RequestMethod {
|
pub enum RequestMethod {
|
||||||
Get,
|
Get,
|
||||||
|
|||||||
@@ -353,6 +353,48 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_handle_request_non_success_code_uses_json_message_for_error_body() {
|
||||||
|
let mut server = Server::new_async().await;
|
||||||
|
let async_server = server
|
||||||
|
.mock("GET", "/test")
|
||||||
|
.match_header("X-Api-Key", "test1234")
|
||||||
|
.with_status(500)
|
||||||
|
.with_body(json!({
|
||||||
|
"message": "database is locked\ndatabase is locked",
|
||||||
|
"description": "at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker"
|
||||||
|
}).to_string())
|
||||||
|
.create_async()
|
||||||
|
.await;
|
||||||
|
let app_arc = Arc::new(Mutex::new(App::test_default()));
|
||||||
|
let mut network = test_network(&app_arc);
|
||||||
|
|
||||||
|
let resp = network
|
||||||
|
.handle_request::<(), Test>(
|
||||||
|
RequestProps {
|
||||||
|
uri: format!("{}/test", server.url()),
|
||||||
|
method: RequestMethod::Get,
|
||||||
|
body: None,
|
||||||
|
api_token: "test1234".to_owned(),
|
||||||
|
ignore_status_code: false,
|
||||||
|
custom_headers: HeaderMap::new(),
|
||||||
|
},
|
||||||
|
|response, mut app| app.error = HorizontallyScrollableText::from(response.value),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
async_server.assert_async().await;
|
||||||
|
assert_str_eq!(
|
||||||
|
app_arc.lock().await.error.text,
|
||||||
|
"Request failed. Received 500 Internal Server Error response code with body: database is locked database is locked"
|
||||||
|
);
|
||||||
|
assert!(resp.is_err());
|
||||||
|
assert_str_eq!(
|
||||||
|
resp.unwrap_err().to_string(),
|
||||||
|
"Request failed. Received 500 Internal Server Error response code with body: database is locked database is locked"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_call_api(
|
async fn test_call_api(
|
||||||
|
|||||||
Reference in New Issue
Block a user