Hi there,
I'm facing an issue with my code in actix+sqlx, I feel it's not performant at all as I have to use block_on but required in my code.
Here is an example:
pub async fn get_my_children_with_toys(current_user: AuthenticatedUser, db_pool: web::Data<PgPool>) -> HttpResponse {
match UserLight::get_my_children(current_user.user_id, &db_pool)
.await
.map(|api_response| {
let content: Vec<FullChildUser> =
api_response.content
.into_iter()
.map(|child| {
let child_id = child.id;
ChildrenWithToys {
child,
toys: tokio::runtime::Runtime::new().unwrap().block_on(
Toys::get_all(child_id, &db_pool)
).unwrap_or(ApiResponse::no_content()) // renvoie Toys[]
}
}).collect();
return ApiResponse::build(content);
}) {
Ok(response) => HttpResponse::Ok().json(response),
Err(error) => HttpResponse::build(error.status).json(error)
}
In my example I have a first feature on which I can use await but later on when I retrieve the list of child (vector), I want to iter over it to fetch toys from DB still await cannot work as I'm not in an async bloc (didn't find a way to write a functional async block with everything).
This is where I find it not ellegant at all and maybe not efficient either. I only found the block_on method to being able to write something that compile.
NB: My real code is something similar with in fact 3 futures on which I tried a join3 but with a block_on too.
Is there any solution to avoid writing a block like that? Should I use tokio or something else?
Maybe I could return a feature instead of a simple HttpResponse but I didn't succeeded in finding something that compile (actix documentation and some tries on my side didn't pay)
Thanks in advance, feel free to provide any feedback as I'm a beginner in Rust.