api2/reader: asyncify the reader worker task

this way, the code is much more readable

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-01-26 11:17:02 +01:00 committed by Dietmar Maurer
parent 41dacd5d3d
commit bdb6e6b83f
1 changed files with 27 additions and 33 deletions

View File

@ -115,7 +115,9 @@ fn upgrade_to_backup_reader_protocol(
let worker_id = format!("{}:{}/{}/{:08X}", store, backup_type, backup_id, backup_dir.backup_time()); let worker_id = format!("{}:{}/{}/{:08X}", store, backup_type, backup_id, backup_dir.backup_time());
WorkerTask::spawn("reader", Some(worker_id), auth_id.clone(), true, move |worker| { WorkerTask::spawn("reader", Some(worker_id), auth_id.clone(), true, move |worker| async move {
let _guard = _guard;
let mut env = ReaderEnvironment::new( let mut env = ReaderEnvironment::new(
env_type, env_type,
auth_id, auth_id,
@ -130,42 +132,34 @@ fn upgrade_to_backup_reader_protocol(
let service = H2Service::new(env.clone(), worker.clone(), &READER_API_ROUTER, debug); let service = H2Service::new(env.clone(), worker.clone(), &READER_API_ROUTER, debug);
let abort_future = worker.abort_future(); let mut abort_future = worker.abort_future()
.map(|_| Err(format_err!("task aborted")));
let req_fut = hyper::upgrade::on(Request::from_parts(parts, req_body)) let env2 = env.clone();
.map_err(Error::from) let req_fut = async move {
.and_then({ let conn = hyper::upgrade::on(Request::from_parts(parts, req_body)).await?;
let env = env.clone(); env2.debug("protocol upgrade done");
move |conn| {
env.debug("protocol upgrade done");
let mut http = hyper::server::conn::Http::new(); let mut http = hyper::server::conn::Http::new();
http.http2_only(true); http.http2_only(true);
// increase window size: todo - find optiomal size // increase window size: todo - find optiomal size
let window_size = 32*1024*1024; // max = (1 << 31) - 2 let window_size = 32*1024*1024; // max = (1 << 31) - 2
http.http2_initial_stream_window_size(window_size); http.http2_initial_stream_window_size(window_size);
http.http2_initial_connection_window_size(window_size); http.http2_initial_connection_window_size(window_size);
http.http2_max_frame_size(4*1024*1024); http.http2_max_frame_size(4*1024*1024);
http.serve_connection(conn, service) http.serve_connection(conn, service)
.map_err(Error::from) .map_err(Error::from).await
} };
});
let abort_future = abort_future
.map(|_| -> Result<(), anyhow::Error> { Err(format_err!("task aborted")) });
use futures::future::Either; futures::select!{
futures::future::select(req_fut, abort_future) req = req_fut.fuse() => req?,
.map(move |res| { abort = abort_future => abort?,
let _guard = _guard; };
match res {
Either::Left((Ok(_), _)) => Ok(()), env.log("reader finished successfully");
Either::Left((Err(err), _)) => Err(err),
Either::Right((Ok(_), _)) => Ok(()), Ok(())
Either::Right((Err(err), _)) => Err(err),
}
})
.map_ok(move |_| env.log("reader finished successfully"))
})?; })?;
let response = Response::builder() let response = Response::builder()