Skip to content

Commit

Permalink
respond to invalid move requests
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Mar 23, 2024
1 parent 215d408 commit 20c84d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ impl Work {
matches!(self, Work::Analysis { .. })
}

pub fn is_move(&self) -> bool {
matches!(self, Work::Move { .. })
}

pub fn multipv(&self) -> NonZeroU8 {
match *self {
Work::Analysis { multipv, .. } => multipv,
Expand Down
30 changes: 25 additions & 5 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ struct QueueState {
cores: NonZeroUsize,
incoming: VecDeque<Chunk>,
pending: HashMap<BatchId, PendingBatch>,
move_submissions: VecDeque<CompletedBatch>,
move_submissions: VecDeque<MoveSubmission>,
stats_recorder: StatsRecorder,
logger: Logger,
}
Expand Down Expand Up @@ -291,9 +291,12 @@ impl QueueState {
completed.into_analysis(),
);
}
Work::Move { .. } => {
Work::Move { id, .. } => {
self.logger.debug(&log);
self.move_submissions.push_back(completed);
self.move_submissions.push_back(MoveSubmission {
batch_id: id,
best_move: completed.into_best_move(),
});
queue.move_submitted();
}
}
Expand All @@ -315,6 +318,12 @@ impl QueueState {
}
}

#[derive(Debug)]
struct MoveSubmission {
batch_id: BatchId,
best_move: Option<Uci>,
}

#[derive(Debug)]
enum QueueMessage {
Pull { callback: oneshot::Sender<Chunk> },
Expand Down Expand Up @@ -380,11 +389,13 @@ impl QueueActor {
}

async fn handle_acquired_response_body(&mut self, body: AcquireResponseBody) {
let batch_id = body.work.id();
let context = ProgressAt {
batch_id: body.work.id(),
batch_id,
batch_url: body.batch_url(self.api.endpoint()),
position_index: None,
};
let is_move = body.work.is_move();

match IncomingBatch::from_acquired(self.api.endpoint(), body) {
Ok(incoming) => {
Expand All @@ -400,6 +411,15 @@ impl QueueActor {
completed.into_analysis(),
);
}
Err(err) if is_move => {
self.logger
.warn(&format!("Invalid move request {context}: {err:?}"));
let mut state = self.state.lock().await;
state.move_submissions.push_back(MoveSubmission {
batch_id,
best_move: None,
});
}
Err(err) => {
self.logger
.warn(&format!("Ignoring invalid batch {context}: {err:?}"));
Expand All @@ -424,7 +444,7 @@ impl QueueActor {
if let Some(completed) = next {
if let Some(Acquired::Accepted(body)) = self
.api
.submit_move_and_acquire(completed.work.id(), completed.into_best_move())
.submit_move_and_acquire(completed.batch_id, completed.best_move)
.await
{
self.handle_acquired_response_body(body).await;
Expand Down

0 comments on commit 20c84d9

Please sign in to comment.