[GUI] More strict log spam protection

This commit is contained in:
Ben C 2024-07-02 21:02:16 -04:00
parent 98df12b051
commit cc461dceb0
No known key found for this signature in database
4 changed files with 29 additions and 9 deletions

View File

@ -34,7 +34,7 @@ use owmods_core::{
use serde::Serialize;
use tauri::FileDropEvent;
use tauri::{api::dialog, async_runtime, AppHandle, Manager, WindowEvent};
use tokio::{sync::mpsc, try_join};
use tokio::{select, sync::mpsc, try_join};
use typeshare::typeshare;
use crate::events::{CustomEventEmitter, CustomEventEmitterAll, CustomEventTriggerGlobal, Event};
@ -761,11 +761,25 @@ pub async fn run_game(
let (tx, mut rx) = mpsc::channel(32);
let log_handler = async {
while let Some(msg) = rx.recv().await {
loop {
select! {
msg = rx.recv() => {
if let Some(msg) = msg {
let mut game_log = state.game_log.write().await;
if let Some(log_data) = game_log.get_mut(&port) {
log_data.take_message(msg);
}
} else {
break;
}
}
_ = tokio::time::sleep(tokio::time::Duration::from_secs(5)) => {
let mut game_log = state.game_log.write().await;
if let Some(log_data) = game_log.get_mut(&port) {
log_data.process_emit_queue();
}
}
}
}
Ok(())
};

View File

@ -68,7 +68,7 @@ pub struct LogData {
}
impl LogData {
const LOG_LIMIT_PER_SECOND: u32 = 25;
const LOG_LIMIT_PER_SECOND: u32 = 5;
const LOG_TIME_UNTIL_FORCED_EMIT_SEC: u64 = 1;
pub fn new(port: LogPort, handle: &AppHandle) -> Result<Self> {

View File

@ -26,12 +26,13 @@ export const useTauri = <T, E extends Event["name"]>(
const errorBound = useErrorBoundary();
useEffect(() => {
const subscriptions: (() => void)[] = [];
if (status !== "Loading") {
for (const eventToSubscribe of events) {
listen(eventToSubscribe, (params) => {
subscriptions.push(listen(eventToSubscribe, (params) => {
if (shouldChangeFn && !shouldChangeFn(params)) return;
setStatus("Loading");
});
}));
}
} else {
commandFn()
@ -44,6 +45,11 @@ export const useTauri = <T, E extends Event["name"]>(
})
.finally(() => setStatus("Done"));
}
return () => {
for (const unsub of subscriptions) {
unsub();
}
};
}, [commandFn, shouldChangeFn, errorBound, events, status]);
useEffect(() => {

View File

@ -12,7 +12,7 @@ pub fn spam_logs(port: u16) -> Result<()> {
println!("Message {i}, {}", i == usize::MAX);
// I just want to easily change stuff when testing so im leaving the format here
#[allow(clippy::useless_format)]
let msg = format!("{{\"type\": 0, \"message\": \"Line 1\", \"senderName\": \"xtask\", \"senderType\": \"log_spammer\"}}\n");
let msg = format!("{{\"type\": 0, \"message\": \"Line {i}\", \"senderName\": \"xtask\", \"senderType\": \"log_spammer\"}}\n");
stream.write_all(msg.as_bytes())?;
i += 1;
std::thread::sleep(Duration::from_secs_f32(