From 3d36e3a514b0cfafcd00edc880b83d0eae87fed7 Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Fri, 19 Aug 2022 15:54:04 -0700 Subject: ConnectionInfo move to bind_front There's a few last calls that are inline lambdas. As we've been moving away from inline lambdas due to the problems with debug logging and usability, this file is the next cleanup. Tested: Ran Redfish-Event-Listener, and set an event into /var/log/redfish with ``` echo "2022-08-04T21:37:49.156798+00:00 OpenBMC.0.1.ServiceFailure,com.intel.DomainMapper.service" > /var/log/redfish ``` Simulating a crash. Saw the event get sent to Redfish-Event-Listener. Signed-off-by: Ed Tanous Change-Id: I61361eef03d1301dc126a28c695fdd74e504f891 --- http/http_client.hpp | 118 +++++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 59 deletions(-) diff --git a/http/http_client.hpp b/http/http_client.hpp index 6bb9159d0e..ae8bfeb09f 100644 --- a/http/http_client.hpp +++ b/http/http_client.hpp @@ -157,30 +157,25 @@ class ConnectionInfo : public std::enable_shared_from_this << std::to_string(port) << ", id: " << std::to_string(connId); - auto respHandler = - [self(shared_from_this())]( - const boost::beast::error_code ec, - const std::vector& - endpointList) { - if (ec || (endpointList.empty())) - { - BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message(); - self->state = ConnState::resolveFailed; - self->waitAndRetry(); - return; - } - BMCWEB_LOG_DEBUG << "Resolved " << self->host << ":" - << std::to_string(self->port) - << ", id: " << std::to_string(self->connId); - self->doConnect(endpointList); - }; - - resolver.asyncResolve(host, port, std::move(respHandler)); + resolver.asyncResolve(host, port, + std::bind_front(&ConnectionInfo::afterResolve, + this, shared_from_this())); } - void doConnect( + void afterResolve( + const std::shared_ptr& /*self*/, + const boost::beast::error_code ec, const std::vector& endpointList) { + if (ec || (endpointList.empty())) + { + BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message(); + state = ConnState::resolveFailed; + waitAndRetry(); + return; + } + BMCWEB_LOG_DEBUG << "Resolved " << host << ":" << std::to_string(port) + << ", id: " << std::to_string(connId); state = ConnState::connectInProgress; BMCWEB_LOG_DEBUG << "Trying to connect to: " << host << ":" @@ -436,24 +431,28 @@ class ConnectionInfo : public std::enable_shared_from_this retryPolicy.retryIntervalSecs.count()) << " seconds. RetryCount = " << retryCount; timer.expires_after(retryPolicy.retryIntervalSecs); - timer.async_wait( - [self(shared_from_this())](const boost::system::error_code ec) { - if (ec == boost::asio::error::operation_aborted) - { - BMCWEB_LOG_DEBUG - << "async_wait failed since the operation is aborted" - << ec.message(); - } - else if (ec) - { - BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message(); - // Ignore the error and continue the retry loop to attempt - // sending the event as per the retry policy - } + timer.async_wait(std::bind_front(&ConnectionInfo::onTimerDone, this, + shared_from_this())); + } - // Let's close the connection and restart from resolve. - self->doClose(true); - }); + void onTimerDone(const std::shared_ptr& /*self*/, + const boost::system::error_code& ec) + { + if (ec == boost::asio::error::operation_aborted) + { + BMCWEB_LOG_DEBUG + << "async_wait failed since the operation is aborted" + << ec.message(); + } + else if (ec) + { + BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message(); + // Ignore the error and continue the retry loop to attempt + // sending the event as per the retry policy + } + + // Let's close the connection and restart from resolve. + doClose(true); } void shutdownConn(bool retry) @@ -691,27 +690,6 @@ class ConnectionPool : public std::enable_shared_from_this const RetryPolicyData& retryPolicy, const std::function& resHandler) { - std::weak_ptr weakSelf = weak_from_this(); - - // Callback to be called once the request has been sent - auto cb = [weakSelf, resHandler](bool keepAlive, uint32_t connId, - Response& res) { - // Allow provided callback to perform additional processing of the - // request - resHandler(res); - - // If requests remain in the queue then we want to reuse this - // connection to send the next request - std::shared_ptr self = weakSelf.lock(); - if (!self) - { - BMCWEB_LOG_CRITICAL << self << " Failed to capture connection"; - return; - } - - self->sendNext(keepAlive, connId); - }; - // Construct the request to be sent boost::beast::http::request thisReq( verb, destUri, 11, "", httpHeader); @@ -719,7 +697,8 @@ class ConnectionPool : public std::enable_shared_from_this thisReq.keep_alive(true); thisReq.body() = std::move(data); thisReq.prepare_payload(); - + auto cb = std::bind_front(&ConnectionPool::afterSendData, + weak_from_this(), resHandler); // Reuse an existing connection if one is available for (unsigned int i = 0; i < connections.size(); i++) { @@ -775,6 +754,27 @@ class ConnectionPool : public std::enable_shared_from_this } } + // Callback to be called once the request has been sent + static void afterSendData(const std::weak_ptr& weakSelf, + const std::function& resHandler, + bool keepAlive, uint32_t connId, Response& res) + { + // Allow provided callback to perform additional processing of the + // request + resHandler(res); + + // If requests remain in the queue then we want to reuse this + // connection to send the next request + std::shared_ptr self = weakSelf.lock(); + if (!self) + { + BMCWEB_LOG_CRITICAL << self << " Failed to capture connection"; + return; + } + + self->sendNext(keepAlive, connId); + } + std::shared_ptr& addConnection() { unsigned int newId = static_cast(connections.size()); -- cgit v1.2.3