spawn extra compute threads

This commit is contained in:
Max Känner 2024-10-03 05:09:04 +02:00
parent 1c9d07f2cf
commit d28eec7ef2

View File

@ -1,11 +1,15 @@
#![allow(clippy::needless_pass_by_value)] #![allow(clippy::needless_pass_by_value)]
use enum_iterator::Sequence; use enum_iterator::Sequence;
use log::info; use log::{error, info};
use rocket::fairing::AdHoc; use rocket::{
use rocket::http::Status; fairing::AdHoc,
use rocket::serde::{json::Json, Deserialize}; get,
use rocket::{get, launch, post, routes}; http::Status,
launch, post, routes,
serde::{json::Json, Deserialize},
tokio::task,
};
use serde::Serialize; use serde::Serialize;
use serde_json::Value; use serde_json::Value;
use std::env; use std::env;
@ -208,13 +212,19 @@ fn handle_start(start_req: Json<GameState>) -> Status {
} }
#[post("/move", format = "json", data = "<move_req>")] #[post("/move", format = "json", data = "<move_req>")]
fn handle_move(move_req: Json<GameState>) -> Option<Json<Action>> { async fn handle_move(move_req: Json<GameState>) -> Option<Json<Action>> {
let response = logic::get_move( let response = task::spawn_blocking(move || {
&move_req.game, logic::get_move(
move_req.turn, &move_req.game,
&move_req.board, move_req.turn,
&move_req.you, &move_req.board,
)?; &move_req.you,
)
})
.await
.inspect_err(|e| error!("failed to join compute thread: {e}"))
.ok()
.flatten()?;
Some(Json(response)) Some(Json(response))
} }