diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 8430bb77..e670edae 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -1311,8 +1311,10 @@ dependencies = [ "derivative", "diesel", "futures-util", + "governor", "lazy_static", "log", + "nonzero_ext", "oauth2", "once_cell", "pgn-reader", @@ -1608,6 +1610,21 @@ dependencies = [ "new_debug_unreachable", ] +[[package]] +name = "futures" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.30" @@ -1615,6 +1632,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -1691,12 +1709,19 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" +[[package]] +name = "futures-timer" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f288b0a4f20f9a56b5d1da57e2227c661b7b16168e2f72365f57b63326e29b24" + [[package]] name = "futures-util" version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ + "futures-channel", "futures-core", "futures-io", "futures-macro", @@ -1971,6 +1996,26 @@ dependencies = [ "system-deps 6.2.0", ] +[[package]] +name = "governor" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68a7f542ee6b35af73b06abc0dad1c1bae89964e4e253bc4b587b91c9637867b" +dependencies = [ + "cfg-if", + "dashmap", + "futures", + "futures-timer", + "no-std-compat", + "nonzero_ext", + "parking_lot", + "portable-atomic", + "quanta", + "rand 0.8.5", + "smallvec", + "spinning_top", +] + [[package]] name = "gtk" version = "0.15.5" @@ -2779,6 +2824,12 @@ dependencies = [ "libc", ] +[[package]] +name = "no-std-compat" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b93853da6d84c2e3c7d730d6473e8817692dd89be387eb01b94d7f108ecb5b8c" + [[package]] name = "nodrop" version = "0.1.14" @@ -2795,6 +2846,12 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nonzero_ext" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38bf9645c8b145698bb0b18a4637dcacbc421ea49bef2317e4fd8065a387cf21" + [[package]] name = "notify-rust" version = "4.10.0" @@ -3455,6 +3512,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "portable-atomic" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7170ef9988bc169ba16dd36a7fa041e5c4cbeb6a35b76d4c03daded371eae7c0" + [[package]] name = "powerfmt" version = "0.2.0" @@ -3551,6 +3614,21 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "quanta" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e5167a477619228a0b284fac2674e3c388cba90631d7b7de620e6f1fcd08da5" +dependencies = [ + "crossbeam-utils", + "libc", + "once_cell", + "raw-cpuid", + "wasi 0.11.0+wasi-snapshot-preview1", + "web-sys", + "winapi", +] + [[package]] name = "quick-xml" version = "0.30.0" @@ -3677,6 +3755,15 @@ dependencies = [ "rand_core 0.5.1", ] +[[package]] +name = "raw-cpuid" +version = "11.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d86a7c4638d42c44551f4791a20e687dbb4c3de1f33c43dd71e355cd429def1" +dependencies = [ + "bitflags 2.4.2", +] + [[package]] name = "raw-window-handle" version = "0.5.2" @@ -4428,6 +4515,15 @@ version = "0.9.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67" +[[package]] +name = "spinning_top" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d96d2d1d716fb500937168cc09353ffdc7a012be8475ac7308e1bdf0e3923300" +dependencies = [ + "lock_api", +] + [[package]] name = "stable_deref_trait" version = "1.2.0" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 9717e3d7..792f3f06 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -57,6 +57,8 @@ axum = "0.6.20" tar = "0.4.40" sysinfo = "0.29.10" window-shadows = "0.2.2" +governor = "0.6.3" +nonzero_ext = "0.3.0" [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/chess.rs b/src-tauri/src/chess.rs index 89d5bc64..97138747 100644 --- a/src-tauri/src/chess.rs +++ b/src-tauri/src/chess.rs @@ -1,7 +1,9 @@ use std::{fmt::Display, path::PathBuf, process::Stdio, sync::Arc, time::Instant}; use derivative::Derivative; +use governor::{Quota, RateLimiter}; use log::{error, info}; +use nonzero_ext::*; use serde::{Deserialize, Serialize}; use shakmaty::{ fen::Fen, san::SanPlus, uci::Uci, ByColor, CastlingMode, Chess, Color, EnPassantMode, Position, @@ -505,6 +507,8 @@ pub async fn get_best_moves( state.engine_processes.insert(key.clone(), process.clone()); + let mut lim = RateLimiter::direct(Quota::per_second(nonzero!(5u32))); + while let Some(line) = reader.next_line().await? { let mut proc = process.lock().await; match parse_one(&line) { @@ -520,6 +524,7 @@ pub async fn get_best_moves( if multipv == proc.real_multipv { if proc.best_moves.iter().all(|x| x.depth == cur_depth) && cur_depth >= proc.last_depth + && lim.check().is_ok() { let progress = match proc.go_mode { GoMode::Depth(depth) => { diff --git a/src/components/panels/analysis/BestMoves.tsx b/src/components/panels/analysis/BestMoves.tsx index e3ad413b..dfdfc0c9 100644 --- a/src/components/panels/analysis/BestMoves.tsx +++ b/src/components/panels/analysis/BestMoves.tsx @@ -436,7 +436,7 @@ function BestMovesComponent({ engineVariations.map((engineVariation, index) => { return (