From 3231966aacb9f869b7d2ed0961f7ea785fdbd2fe Mon Sep 17 00:00:00 2001 From: Pavel Emelyanov Date: Thu, 12 Sep 2024 11:26:33 +0300 Subject: [PATCH] http/client: Coroutinize client::do_make_request (con-less overload) It makes some non-trivial decisions on whether or to retry the request in the catch block, so the conversion is a bit hairy. Signed-off-by: Pavel Emelyanov --- src/http/client.cc | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/http/client.cc b/src/http/client.cc index c72cdd6168..4f8da7b6dd 100644 --- a/src/http/client.cc +++ b/src/http/client.cc @@ -307,31 +307,31 @@ future<> client::make_request(request req, reply_handler handle, abort_source& a } future<> client::do_make_request(request req, reply_handler handle, abort_source* as, std::optional expected) { - return do_with(std::move(req), std::move(handle), [this, as, expected] (request& req, reply_handler& handle) mutable { - return with_connection([this, &req, &handle, as, expected] (connection& con) { - return do_make_request(con, req, handle, as, expected); - }, as).handle_exception_type([this, &req, &handle, as, expected] (const std::system_error& ex) { + try { + co_return co_await with_connection(coroutine::lambda([this, &req, &handle, as, expected] (connection& con) -> future<> { + co_await do_make_request(con, req, handle, as, expected); + }), as); + } catch (const std::system_error& ex) { if (as && as->abort_requested()) { - return make_exception_future<>(as->abort_requested_exception_ptr()); + std::rethrow_exception(as->abort_requested_exception_ptr()); } if (!_retry) { - return make_exception_future<>(ex); + throw; } auto code = ex.code().value(); if ((code != EPIPE) && (code != ECONNABORTED)) { - return make_exception_future<>(ex); + throw; } + } // The 'con' connection may not yet be freed, so the total connection // count still account for it and with_new_connection() may temporarily // break the limit. That's OK, the 'con' will be closed really soon - return with_new_connection([this, &req, &handle, as, expected] (connection& con) { - return do_make_request(con, req, handle, as, expected); - }, as); - }); - }); + co_await with_new_connection(coroutine::lambda([this, &req, &handle, as, expected] (connection& con) -> future<> { + co_await do_make_request(con, req, handle, as, expected); + }), as); } future<> client::do_make_request(connection& con, request& req, reply_handler& handle, abort_source* as, std::optional expected) {