rewrite future select in upgrade_to_backup_protocol using select macro

and handle all ok/err cases with at least logging

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2020-03-18 11:32:51 +01:00 committed by Wolfgang Bumiller
parent 66b4593b04
commit 6650a242fb

View File

@ -106,13 +106,12 @@ fn upgrade_to_backup_protocol(
let abort_future = worker.abort_future(); let abort_future = worker.abort_future();
let env2 = env.clone(); let env2 = env.clone();
let env3 = env.clone();
let req_fut = req_body let mut req_fut = req_body
.on_upgrade() .on_upgrade()
.map_err(Error::from) .map_err(Error::from)
.and_then(move |conn| { .and_then(move |conn| {
env3.debug("protocol upgrade done"); env2.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);
@ -124,36 +123,39 @@ fn upgrade_to_backup_protocol(
http.serve_connection(conn, service) http.serve_connection(conn, service)
.map_err(Error::from) .map_err(Error::from)
}); });
let abort_future = abort_future let mut abort_future = abort_future
.map(|_| Err(format_err!("task aborted"))); .map(|_| Err(format_err!("task aborted")));
use futures::future::Either; async move {
future::select(req_fut, abort_future) let res = select!{
.map(|res| match res { req = req_fut => req,
Either::Left((Ok(res), _)) => Ok(res), abrt = abort_future => abrt,
Either::Left((Err(err), _)) => Err(err), };
Either::Right((Ok(res), _)) => Ok(res),
Either::Right((Err(err), _)) => Err(err), match (res, env.ensure_finished()) {
}) (Ok(_), Ok(())) => {
.and_then(move |_result| async move { env.log("backup finished sucessfully");
env.ensure_finished()?; Ok(())
env.log("backup finished sucessfully"); },
Ok(()) (Err(err), Ok(())) => {
}) // ignore errors after finish
.then(move |result| async move { env.log(format!("backup had errors but finished: {}", err));
if let Err(err) = result { Ok(())
match env2.ensure_finished() { },
Ok(()) => {}, // ignore error after finish (Ok(_), Err(err)) => {
_ => { env.log(format!("backup ended and finish failed: {}", err));
env2.log(format!("backup failed: {}", err)); env.log("removing unfinished backup");
env2.log("removing failed backup"); env.remove_backup()?;
env2.remove_backup()?; Err(err)
return Err(err); },
} (Err(err), Err(_)) => {
} env.log(format!("backup failed: {}", err));
} env.log("removing failed backup");
Ok(()) env.remove_backup()?;
}) Err(err)
},
}
}
})?; })?;
let response = Response::builder() let response = Response::builder()