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