From 5fb318fd25bb00fe78ceb3a98cafa30a1971e43e Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sat, 19 Sep 2020 10:32:45 +0800 Subject: [PATCH] crimson/common: discard failure of finally() block instead of assuming that the function passed to finally() returns an erroratorized future, in this change: * s/safe_then/then_wrapped/ to handle the exception thrown by the finally function. * specialize for the case where the finally function does not return a future, and just call it. note, in seastar's implementation of finally, `finally_body` is used for specializing these two cases. * rename "future" to "result", for better readability. Signed-off-by: Kefu Chai --- src/crimson/common/errorator.h | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/crimson/common/errorator.h b/src/crimson/common/errorator.h index 16335380192c9..ea0ffea4d4915 100644 --- a/src/crimson/common/errorator.h +++ b/src/crimson/common/errorator.h @@ -592,23 +592,26 @@ private: return seastar::future::get0(); } - template auto finally(FuncT &&func) { - using func_result_t = std::invoke_result_t; - using func_errorator_t = get_errorator_t; - using return_errorator_t = func_errorator_t; - using futurator_t = - typename return_errorator_t::template futurize; - return this->then_wrapped( - [ func = std::forward(func) - ] (auto&& future) mutable noexcept { - return futurator_t::invoke(std::forward(func)).safe_then( - [future = std::forward(future)]() mutable { - return std::move(future); - }); - }); + [func = std::forward(func)](auto &&result) mutable noexcept { + if constexpr (seastar::is_future>::value) { + return ::seastar::futurize_invoke(std::forward(func)).then_wrapped( + [result = std::move(result)](auto&& f_res) mutable { + // TODO: f_res.failed() + f_res.discard_result(); + return std::move(result); + }); + } else { + try { + func(); + } catch (...) { + // TODO: rethrow + } + return std::move(result); + } + }); } // taking ErrorFuncOne and ErrorFuncTwo separately from ErrorFuncTail -- 2.39.5