Skip to content

Commit

Permalink
rate limit engine lines
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoBSalgueiro committed Apr 2, 2024
1 parent 75f93b5 commit 7205409
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
96 changes: 96 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src-tauri/src/chess.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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) {
Expand All @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/panels/analysis/BestMoves.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ function BestMovesComponent({
engineVariations.map((engineVariation, index) => {
return (
<AnalysisRow
key={engineVariation.sanMoves.join(",")}
key={index}
moves={engineVariation.sanMoves}
score={engineVariation.score}
halfMoves={halfMoves}
Expand Down

0 comments on commit 7205409

Please sign in to comment.