From 6650a242fb68b80d9ba1eb292b79f2c663df7542 Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Wed, 18 Mar 2020 11:32:51 +0100 Subject: [PATCH] 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 --- src/api2/backup.rs | 64 ++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/src/api2/backup.rs b/src/api2/backup.rs index 8ce6d29e..a57547a9 100644 --- a/src/api2/backup.rs +++ b/src/api2/backup.rs @@ -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()