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