I am trying to use multiple async functions in a single async function and wait for all the executions before return and finally exit.
My understanding is not enough to make the compiler happy.
The textbook example works.
use futures::join;
use lambda_http::{run, service_fn, Body, Error, Request, Response};
async fn one() -> i32 {
1
}
async fn two() -> i32 {
2
}
async fn function_handler(_event: Request) -> Result<Response<Body>, Error> {
let (_one, _two) = join!(one(), two());
let resp = Response::builder()
.status(200)
.header("content-type", "application/json")
.body("Hello AWS Lambda HTTP request".into())
.map_err(Box::new)?;
Ok(resp)
}
#[tokio::main]
async fn main() -> Result<(), Error> {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::INFO)
.with_target(false)
.without_time()
.init();
run(service_fn(function_handler)).await
}
What does not work is:
One of the async functions that I would like join later on:
pub async fn create_db_eid_cache(
aws_clients: AwsClients,
config: &Config,
) -> Result<String, Box<dyn error::Error>> {
let db_names = get_all_database_names(aws_clients.glue_client).await?;
let db_eid_cache_opt = create_eid_cache(db_names);
if db_eid_cache_opt.is_none() {
return Ok("There is no database in this catalog".to_string());
}
let db_eid_cache = db_eid_cache_opt.unwrap();
let db_eid_cache_json = serde_json::to_string(&db_eid_cache)?;
let s3_path_prefix = get_cache_key("db-eid-cache");
match s3_put_object_bytes(
aws_clients.s3_client,
&config.target_s3_bucket,
&s3_path_prefix,
&db_eid_cache_json,
)
.await
{
Ok(_v) => Ok("Successfully updated cache".to_string()),
Err(e) => Err(Box::from(e)),
}
}
The error:
cache_manager
async fn function_handler(_event: Request) -> Result<Response<Body>, Error>
Go to Future | Sized
future cannot be sent between threads safely
the trait `std::marker::Send` is not implemented for `(dyn StdError + 'static)`rustcClick for full compiler diagnostic
join_mod.rs(95, 13): future is not `Send` as this value is used across an await
join_mod.rs(97, 13): the value is later dropped here
lib.rs(185, 16): required by a bound in `lambda_http::run`
This is the official solution:
https://rust-lang.github.io/async-book/07_workarounds/03_send_approximation.html
I am not sure how to do this in the context of my function.