AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2020 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 17 | #include <boost/asio/ip/address.hpp> |
| 18 | #include <boost/asio/ip/basic_endpoint.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 19 | #include <boost/asio/steady_timer.hpp> |
| 20 | #include <boost/beast/core/flat_buffer.hpp> |
| 21 | #include <boost/beast/core/tcp_stream.hpp> |
| 22 | #include <boost/beast/http/message.hpp> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 23 | #include <boost/beast/version.hpp> |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 24 | #include <boost/container/devector.hpp> |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 25 | #include <include/async_resolve.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 26 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 27 | #include <cstdlib> |
| 28 | #include <functional> |
| 29 | #include <iostream> |
| 30 | #include <memory> |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 31 | #include <queue> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 32 | #include <string> |
| 33 | |
| 34 | namespace crow |
| 35 | { |
| 36 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 37 | // It is assumed that the BMC should be able to handle 4 parallel connections |
| 38 | constexpr uint8_t maxPoolSize = 4; |
| 39 | constexpr uint8_t maxRequestQueueSize = 50; |
| 40 | constexpr unsigned int httpReadBodyLimit = 8192; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 41 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 42 | enum class ConnState |
| 43 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 44 | initialized, |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 45 | resolveInProgress, |
| 46 | resolveFailed, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 47 | connectInProgress, |
| 48 | connectFailed, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 49 | connected, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 50 | sendInProgress, |
| 51 | sendFailed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 52 | recvInProgress, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 53 | recvFailed, |
| 54 | idle, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 55 | closeInProgress, |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 56 | closed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 57 | suspended, |
| 58 | terminated, |
| 59 | abortConnection, |
| 60 | retry |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 61 | }; |
| 62 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 63 | // We need to allow retry information to be set before a message has been sent |
| 64 | // and a connection pool has been created |
| 65 | struct RetryPolicyData |
| 66 | { |
| 67 | uint32_t maxRetryAttempts = 5; |
| 68 | std::chrono::seconds retryIntervalSecs = std::chrono::seconds(0); |
| 69 | std::string retryPolicyAction = "TerminateAfterRetries"; |
| 70 | std::string name; |
| 71 | }; |
| 72 | |
| 73 | struct PendingRequest |
| 74 | { |
| 75 | std::string requestData; |
| 76 | std::function<void(bool, uint32_t)> callback; |
| 77 | RetryPolicyData retryPolicy; |
| 78 | PendingRequest(const std::string& requestData, |
| 79 | const std::function<void(bool, uint32_t)>& callback, |
| 80 | const RetryPolicyData& retryPolicy) : |
| 81 | requestData(requestData), |
| 82 | callback(callback), retryPolicy(retryPolicy) |
| 83 | {} |
| 84 | }; |
| 85 | |
| 86 | class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 87 | { |
| 88 | private: |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 89 | ConnState state = ConnState::initialized; |
| 90 | uint32_t retryCount = 0; |
| 91 | bool runningTimer = false; |
| 92 | std::string subId; |
| 93 | std::string host; |
| 94 | uint16_t port; |
| 95 | uint32_t connId; |
| 96 | |
| 97 | // Retry policy information |
| 98 | // This should be updated before each message is sent |
| 99 | RetryPolicyData retryPolicy; |
| 100 | |
| 101 | // Data buffers |
| 102 | std::string data; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 103 | boost::beast::http::request<boost::beast::http::string_body> req; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 104 | std::optional< |
| 105 | boost::beast::http::response_parser<boost::beast::http::string_body>> |
| 106 | parser; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 107 | boost::beast::flat_static_buffer<httpReadBodyLimit> buffer; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 108 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 109 | // Ascync callables |
| 110 | std::function<void(bool, uint32_t)> callback; |
| 111 | crow::async_resolve::Resolver resolver; |
| 112 | boost::beast::tcp_stream conn; |
| 113 | boost::asio::steady_timer timer; |
Ed Tanous | 84b3560 | 2021-09-08 20:06:32 -0700 | [diff] [blame] | 114 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 115 | friend class ConnectionPool; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 116 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 117 | void doResolve() |
| 118 | { |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 119 | state = ConnState::resolveInProgress; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 120 | BMCWEB_LOG_DEBUG << "Trying to resolve: " << host << ":" |
| 121 | << std::to_string(port) |
| 122 | << ", id: " << std::to_string(connId); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 123 | |
| 124 | auto respHandler = |
| 125 | [self(shared_from_this())]( |
| 126 | const boost::beast::error_code ec, |
| 127 | const std::vector<boost::asio::ip::tcp::endpoint>& |
| 128 | endpointList) { |
Ed Tanous | 26f6976 | 2022-01-25 09:49:11 -0800 | [diff] [blame] | 129 | if (ec || (endpointList.empty())) |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 130 | { |
| 131 | BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message(); |
| 132 | self->state = ConnState::resolveFailed; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 133 | self->waitAndRetry(); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 134 | return; |
| 135 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 136 | BMCWEB_LOG_DEBUG << "Resolved " << self->host << ":" |
| 137 | << std::to_string(self->port) |
| 138 | << ", id: " << std::to_string(self->connId); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 139 | self->doConnect(endpointList); |
| 140 | }; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 141 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 142 | resolver.asyncResolve(host, port, std::move(respHandler)); |
| 143 | } |
| 144 | |
| 145 | void doConnect( |
| 146 | const std::vector<boost::asio::ip::tcp::endpoint>& endpointList) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 147 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 148 | state = ConnState::connectInProgress; |
| 149 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 150 | BMCWEB_LOG_DEBUG << "Trying to connect to: " << host << ":" |
| 151 | << std::to_string(port) |
| 152 | << ", id: " << std::to_string(connId); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 153 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 154 | conn.expires_after(std::chrono::seconds(30)); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 155 | conn.async_connect( |
| 156 | endpointList, [self(shared_from_this())]( |
| 157 | const boost::beast::error_code ec, |
| 158 | const boost::asio::ip::tcp::endpoint& endpoint) { |
| 159 | if (ec) |
| 160 | { |
Ed Tanous | 8cc8ede | 2022-02-28 10:20:59 -0800 | [diff] [blame] | 161 | BMCWEB_LOG_ERROR << "Connect " |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 162 | << endpoint.address().to_string() << ":" |
| 163 | << std::to_string(endpoint.port()) |
| 164 | << ", id: " << std::to_string(self->connId) |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 165 | << " failed: " << ec.message(); |
| 166 | self->state = ConnState::connectFailed; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 167 | self->waitAndRetry(); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 168 | return; |
| 169 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 170 | BMCWEB_LOG_DEBUG |
| 171 | << "Connected to: " << endpoint.address().to_string() << ":" |
| 172 | << std::to_string(endpoint.port()) |
| 173 | << ", id: " << std::to_string(self->connId); |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 174 | self->state = ConnState::connected; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 175 | self->sendMessage(); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 176 | }); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 177 | } |
| 178 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 179 | void sendMessage() |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 180 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 181 | state = ConnState::sendInProgress; |
| 182 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 183 | req.body() = data; |
| 184 | req.prepare_payload(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 185 | |
| 186 | // Set a timeout on the operation |
| 187 | conn.expires_after(std::chrono::seconds(30)); |
| 188 | |
| 189 | // Send the HTTP request to the remote host |
| 190 | boost::beast::http::async_write( |
| 191 | conn, req, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 192 | [self(shared_from_this())](const boost::beast::error_code& ec, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 193 | const std::size_t& bytesTransferred) { |
| 194 | if (ec) |
| 195 | { |
| 196 | BMCWEB_LOG_ERROR << "sendMessage() failed: " |
| 197 | << ec.message(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 198 | self->state = ConnState::sendFailed; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 199 | self->waitAndRetry(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | BMCWEB_LOG_DEBUG << "sendMessage() bytes transferred: " |
| 203 | << bytesTransferred; |
| 204 | boost::ignore_unused(bytesTransferred); |
| 205 | |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 206 | self->recvMessage(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 207 | }); |
| 208 | } |
| 209 | |
| 210 | void recvMessage() |
| 211 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 212 | state = ConnState::recvInProgress; |
| 213 | |
| 214 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
| 215 | parser->body_limit(httpReadBodyLimit); |
| 216 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 217 | // Receive the HTTP response |
| 218 | boost::beast::http::async_read( |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 219 | conn, buffer, *parser, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 220 | [self(shared_from_this())](const boost::beast::error_code& ec, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 221 | const std::size_t& bytesTransferred) { |
| 222 | if (ec) |
| 223 | { |
| 224 | BMCWEB_LOG_ERROR << "recvMessage() failed: " |
| 225 | << ec.message(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 226 | self->state = ConnState::recvFailed; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 227 | self->waitAndRetry(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | BMCWEB_LOG_DEBUG << "recvMessage() bytes transferred: " |
| 231 | << bytesTransferred; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 232 | BMCWEB_LOG_DEBUG << "recvMessage() data: " |
Ed Tanous | 8cc8ede | 2022-02-28 10:20:59 -0800 | [diff] [blame] | 233 | << self->parser->get().body(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 234 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 235 | unsigned int respCode = self->parser->get().result_int(); |
| 236 | BMCWEB_LOG_DEBUG << "recvMessage() Header Response Code: " |
| 237 | << respCode; |
| 238 | |
| 239 | // 2XX response is considered to be successful |
| 240 | if ((respCode < 200) || (respCode >= 300)) |
| 241 | { |
| 242 | // The listener failed to receive the Sent-Event |
Sunitha Harish | 7adb85a | 2021-10-26 03:10:04 -0500 | [diff] [blame] | 243 | BMCWEB_LOG_ERROR |
| 244 | << "recvMessage() Listener Failed to " |
| 245 | "receive Sent-Event. Header Response Code: " |
| 246 | << respCode; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 247 | self->state = ConnState::recvFailed; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 248 | self->waitAndRetry(); |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 249 | return; |
| 250 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 251 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 252 | // Send is successful |
| 253 | // Reset the counter just in case this was after retrying |
| 254 | self->retryCount = 0; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 255 | |
| 256 | // Keep the connection alive if server supports it |
| 257 | // Else close the connection |
| 258 | BMCWEB_LOG_DEBUG << "recvMessage() keepalive : " |
| 259 | << self->parser->keep_alive(); |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 260 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 261 | self->callback(self->parser->keep_alive(), self->connId); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 262 | }); |
| 263 | } |
| 264 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 265 | void waitAndRetry() |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 266 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 267 | if (retryCount >= retryPolicy.maxRetryAttempts) |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 268 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 269 | BMCWEB_LOG_ERROR << "Maximum number of retries reached."; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 270 | BMCWEB_LOG_DEBUG << "Retry policy: " |
| 271 | << retryPolicy.retryPolicyAction; |
| 272 | if (retryPolicy.retryPolicyAction == "TerminateAfterRetries") |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 273 | { |
| 274 | // TODO: delete subscription |
| 275 | state = ConnState::terminated; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 276 | callback(false, connId); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 277 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 278 | if (retryPolicy.retryPolicyAction == "SuspendRetries") |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 279 | { |
| 280 | state = ConnState::suspended; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 281 | callback(false, connId); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 282 | } |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 283 | // Reset the retrycount to zero so that client can try connecting |
| 284 | // again if needed |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 285 | retryCount = 0; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 286 | return; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 287 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 288 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 289 | if (runningTimer) |
| 290 | { |
| 291 | BMCWEB_LOG_DEBUG << "Retry timer is already running."; |
| 292 | return; |
| 293 | } |
| 294 | runningTimer = true; |
| 295 | |
| 296 | retryCount++; |
| 297 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 298 | BMCWEB_LOG_DEBUG << "Attempt retry after " |
| 299 | << std::to_string( |
| 300 | retryPolicy.retryIntervalSecs.count()) |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 301 | << " seconds. RetryCount = " << retryCount; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 302 | timer.expires_after(retryPolicy.retryIntervalSecs); |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 303 | timer.async_wait( |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 304 | [self(shared_from_this())](const boost::system::error_code ec) { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 305 | if (ec == boost::asio::error::operation_aborted) |
| 306 | { |
| 307 | BMCWEB_LOG_DEBUG |
| 308 | << "async_wait failed since the operation is aborted" |
| 309 | << ec.message(); |
| 310 | } |
| 311 | else if (ec) |
| 312 | { |
| 313 | BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message(); |
| 314 | // Ignore the error and continue the retry loop to attempt |
| 315 | // sending the event as per the retry policy |
| 316 | } |
| 317 | self->runningTimer = false; |
| 318 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 319 | // Let's close the connection and restart from resolve. |
| 320 | self->doCloseAndRetry(); |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 321 | }); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 322 | } |
| 323 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 324 | void doClose() |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 325 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 326 | state = ConnState::closeInProgress; |
| 327 | boost::beast::error_code ec; |
| 328 | conn.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); |
| 329 | conn.close(); |
| 330 | |
| 331 | // not_connected happens sometimes so don't bother reporting it. |
| 332 | if (ec && ec != boost::beast::errc::not_connected) |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 333 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 334 | BMCWEB_LOG_ERROR << host << ":" << std::to_string(port) |
| 335 | << ", id: " << std::to_string(connId) |
| 336 | << "shutdown failed: " << ec.message(); |
| 337 | return; |
| 338 | } |
| 339 | BMCWEB_LOG_DEBUG << host << ":" << std::to_string(port) |
| 340 | << ", id: " << std::to_string(connId) |
| 341 | << " closed gracefully"; |
| 342 | if ((state != ConnState::suspended) && (state != ConnState::terminated)) |
| 343 | { |
| 344 | state = ConnState::closed; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void doCloseAndRetry() |
| 349 | { |
| 350 | state = ConnState::closeInProgress; |
| 351 | boost::beast::error_code ec; |
| 352 | conn.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); |
| 353 | conn.close(); |
| 354 | |
| 355 | // not_connected happens sometimes so don't bother reporting it. |
| 356 | if (ec && ec != boost::beast::errc::not_connected) |
| 357 | { |
| 358 | BMCWEB_LOG_ERROR << host << ":" << std::to_string(port) |
| 359 | << ", id: " << std::to_string(connId) |
| 360 | << "shutdown failed: " << ec.message(); |
| 361 | return; |
| 362 | } |
| 363 | BMCWEB_LOG_DEBUG << host << ":" << std::to_string(port) |
| 364 | << ", id: " << std::to_string(connId) |
| 365 | << " closed gracefully"; |
| 366 | if ((state != ConnState::suspended) && (state != ConnState::terminated)) |
| 367 | { |
| 368 | // Now let's try to resend the data |
| 369 | state = ConnState::retry; |
| 370 | this->doResolve(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 371 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | public: |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 375 | explicit ConnectionInfo(boost::asio::io_context& ioc, const std::string& id, |
| 376 | const std::string& destIP, const uint16_t destPort, |
| 377 | const std::string& destUri, |
| 378 | const boost::beast::http::fields& httpHeader, |
| 379 | const unsigned int connId) : |
| 380 | subId(id), |
| 381 | host(destIP), port(destPort), connId(connId), |
Ed Tanous | 4da0445 | 2021-09-08 19:57:44 -0700 | [diff] [blame] | 382 | req(boost::beast::http::verb::post, destUri, 11, "", httpHeader), |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 383 | conn(ioc), timer(ioc) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 384 | { |
Ed Tanous | 4da0445 | 2021-09-08 19:57:44 -0700 | [diff] [blame] | 385 | req.set(boost::beast::http::field::host, host); |
| 386 | req.keep_alive(true); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 387 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 388 | }; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 389 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 390 | class ConnectionPool : public std::enable_shared_from_this<ConnectionPool> |
| 391 | { |
| 392 | private: |
| 393 | boost::asio::io_context& ioc; |
| 394 | const std::string id; |
| 395 | const std::string destIP; |
| 396 | const uint16_t destPort; |
| 397 | const std::string destUri; |
| 398 | const boost::beast::http::fields httpHeader; |
| 399 | std::vector<std::shared_ptr<ConnectionInfo>> connections; |
| 400 | boost::container::devector<PendingRequest> requestQueue; |
| 401 | |
| 402 | friend class HttpClient; |
| 403 | |
| 404 | // Configure a connections's data, callback, and retry info in preparation |
| 405 | // to begin sending a request |
| 406 | void setConnProps(ConnectionInfo& conn) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 407 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 408 | if (requestQueue.empty()) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 409 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 410 | BMCWEB_LOG_ERROR |
| 411 | << "setConnProps() should not have been called when requestQueue is empty"; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 412 | return; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 413 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 414 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 415 | auto req = requestQueue.front(); |
| 416 | conn.retryPolicy = std::move(req.retryPolicy); |
| 417 | conn.data = std::move(req.requestData); |
| 418 | conn.callback = std::move(req.callback); |
| 419 | |
| 420 | BMCWEB_LOG_DEBUG << "Setting properties for connection " << conn.host |
| 421 | << ":" << std::to_string(conn.port) |
| 422 | << ", id: " << std::to_string(conn.connId) |
| 423 | << ", retry policy is \"" << conn.retryPolicy.name |
| 424 | << "\""; |
| 425 | |
| 426 | // We can remove the request from the queue at this point |
| 427 | requestQueue.pop_front(); |
| 428 | } |
| 429 | |
| 430 | // Configures a connection to use the specific retry policy. |
| 431 | inline void setConnRetryPolicy(ConnectionInfo& conn, |
| 432 | const RetryPolicyData& retryPolicy) |
| 433 | { |
| 434 | BMCWEB_LOG_DEBUG << destIP << ":" << std::to_string(destPort) |
| 435 | << ", id: " << std::to_string(conn.connId) |
| 436 | << " using retry policy \"" << retryPolicy.name |
| 437 | << "\""; |
| 438 | |
| 439 | conn.retryPolicy = retryPolicy; |
| 440 | } |
| 441 | |
| 442 | // Gets called as part of callback after request is sent |
| 443 | // Reuses the connection if there are any requests waiting to be sent |
| 444 | // Otherwise closes the connection if it is not a keep-alive |
| 445 | void sendNext(bool keepAlive, uint32_t connId) |
| 446 | { |
| 447 | auto conn = connections[connId]; |
| 448 | // Reuse the connection to send the next request in the queue |
| 449 | if (!requestQueue.empty()) |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 450 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 451 | BMCWEB_LOG_DEBUG << std::to_string(requestQueue.size()) |
| 452 | << " requests remaining in queue for " << destIP |
| 453 | << ":" << std::to_string(destPort) |
| 454 | << ", reusing connnection " |
| 455 | << std::to_string(connId); |
| 456 | |
| 457 | setConnProps(*conn); |
| 458 | |
| 459 | if (keepAlive) |
| 460 | { |
| 461 | conn->sendMessage(); |
| 462 | } |
| 463 | else |
| 464 | { |
| 465 | // Server is not keep-alive enabled so we need to close the |
| 466 | // connection and then start over from resolve |
| 467 | conn->doClose(); |
| 468 | conn->doResolve(); |
| 469 | } |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | // No more messages to send so close the connection if necessary |
| 474 | if (keepAlive) |
| 475 | { |
| 476 | conn->state = ConnState::idle; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 477 | } |
| 478 | else |
| 479 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 480 | // Abort the connection since server is not keep-alive enabled |
| 481 | conn->state = ConnState::abortConnection; |
| 482 | conn->doClose(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 483 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 484 | } |
| 485 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 486 | void sendData(std::string& data, const RetryPolicyData& retryPolicy) |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 487 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 488 | std::weak_ptr<ConnectionPool> weakSelf = weak_from_this(); |
| 489 | |
| 490 | // Callback to be called once the request has been sent |
| 491 | auto cb = [weakSelf](bool keepAlive, uint32_t connId) { |
| 492 | // If requests remain in the queue then we want to reuse this |
| 493 | // connection to send the next request |
| 494 | std::shared_ptr<ConnectionPool> self = weakSelf.lock(); |
| 495 | if (!self) |
| 496 | { |
| 497 | BMCWEB_LOG_CRITICAL << self << " Failed to capture connection"; |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | self->sendNext(keepAlive, connId); |
| 502 | }; |
| 503 | |
| 504 | // Reuse an existing connection if one is available |
| 505 | for (unsigned int i = 0; i < connections.size(); i++) |
| 506 | { |
| 507 | auto conn = connections[i]; |
| 508 | if ((conn->state == ConnState::idle) || |
| 509 | (conn->state == ConnState::initialized) || |
| 510 | (conn->state == ConnState::closed)) |
| 511 | { |
| 512 | conn->data = std::move(data); |
| 513 | conn->callback = std::move(cb); |
| 514 | conn->retryPolicy = retryPolicy; |
| 515 | setConnRetryPolicy(*conn, retryPolicy); |
| 516 | std::string commonMsg = std::to_string(i) + " from pool " + |
| 517 | destIP + ":" + std::to_string(destPort); |
| 518 | |
| 519 | if (conn->state == ConnState::idle) |
| 520 | { |
| 521 | BMCWEB_LOG_DEBUG << "Grabbing idle connection " |
| 522 | << commonMsg; |
| 523 | conn->sendMessage(); |
| 524 | } |
| 525 | else |
| 526 | { |
| 527 | BMCWEB_LOG_DEBUG << "Reusing existing connection " |
| 528 | << commonMsg; |
| 529 | conn->doResolve(); |
| 530 | } |
| 531 | return; |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | // All connections in use so create a new connection or add request to |
| 536 | // the queue |
| 537 | if (connections.size() < maxPoolSize) |
| 538 | { |
| 539 | BMCWEB_LOG_DEBUG << "Adding new connection to pool " << destIP |
| 540 | << ":" << std::to_string(destPort); |
| 541 | auto conn = addConnection(); |
| 542 | conn->data = std::move(data); |
| 543 | conn->callback = std::move(cb); |
| 544 | setConnRetryPolicy(*conn, retryPolicy); |
| 545 | conn->doResolve(); |
| 546 | } |
| 547 | else if (requestQueue.size() < maxRequestQueueSize) |
| 548 | { |
| 549 | BMCWEB_LOG_ERROR << "Max pool size reached. Adding data to queue."; |
| 550 | requestQueue.emplace_back(std::move(data), std::move(cb), |
| 551 | retryPolicy); |
| 552 | } |
| 553 | else |
| 554 | { |
| 555 | BMCWEB_LOG_ERROR << destIP << ":" << std::to_string(destPort) |
| 556 | << " request queue full. Dropping request."; |
| 557 | } |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 558 | } |
| 559 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 560 | std::shared_ptr<ConnectionInfo>& addConnection() |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 561 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 562 | unsigned int newId = static_cast<unsigned int>(connections.size()); |
| 563 | |
| 564 | auto& ret = connections.emplace_back(std::make_shared<ConnectionInfo>( |
| 565 | ioc, id, destIP, destPort, destUri, httpHeader, newId)); |
| 566 | |
| 567 | BMCWEB_LOG_DEBUG << "Added connection " |
| 568 | << std::to_string(connections.size() - 1) |
| 569 | << " to pool " << destIP << ":" |
| 570 | << std::to_string(destPort); |
| 571 | |
| 572 | return ret; |
| 573 | } |
| 574 | |
| 575 | public: |
| 576 | explicit ConnectionPool(boost::asio::io_context& ioc, const std::string& id, |
| 577 | const std::string& destIP, const uint16_t destPort, |
| 578 | const std::string& destUri, |
| 579 | const boost::beast::http::fields& httpHeader) : |
| 580 | ioc(ioc), |
| 581 | id(id), destIP(destIP), destPort(destPort), destUri(destUri), |
| 582 | httpHeader(httpHeader) |
| 583 | { |
| 584 | std::string clientKey = destIP + ":" + std::to_string(destPort); |
| 585 | BMCWEB_LOG_DEBUG << "Initializing connection pool for " << destIP << ":" |
| 586 | << std::to_string(destPort); |
| 587 | |
| 588 | // Initialize the pool with a single connection |
| 589 | addConnection(); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 590 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 591 | }; |
| 592 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame^] | 593 | class HttpClient |
| 594 | { |
| 595 | private: |
| 596 | std::unordered_map<std::string, std::shared_ptr<ConnectionPool>> |
| 597 | connectionPools; |
| 598 | boost::asio::io_context& ioc = |
| 599 | crow::connections::systemBus->get_io_context(); |
| 600 | std::unordered_map<std::string, RetryPolicyData> retryInfo; |
| 601 | HttpClient() = default; |
| 602 | |
| 603 | public: |
| 604 | HttpClient(const HttpClient&) = delete; |
| 605 | HttpClient& operator=(const HttpClient&) = delete; |
| 606 | HttpClient(HttpClient&&) = delete; |
| 607 | HttpClient& operator=(HttpClient&&) = delete; |
| 608 | ~HttpClient() = default; |
| 609 | |
| 610 | static HttpClient& getInstance() |
| 611 | { |
| 612 | static HttpClient handler; |
| 613 | return handler; |
| 614 | } |
| 615 | |
| 616 | void sendData(std::string& data, const std::string& id, |
| 617 | const std::string& destIP, const uint16_t destPort, |
| 618 | const std::string& destUri, |
| 619 | const boost::beast::http::fields& httpHeader, |
| 620 | std::string& retryPolicyName) |
| 621 | { |
| 622 | std::string clientKey = destIP + ":" + std::to_string(destPort); |
| 623 | // Use nullptr to avoid creating a ConnectionPool each time |
| 624 | auto result = connectionPools.try_emplace(clientKey, nullptr); |
| 625 | if (result.second) |
| 626 | { |
| 627 | // Now actually create the ConnectionPool shared_ptr since it does |
| 628 | // not already exist |
| 629 | result.first->second = std::make_shared<ConnectionPool>( |
| 630 | ioc, id, destIP, destPort, destUri, httpHeader); |
| 631 | BMCWEB_LOG_DEBUG << "Created connection pool for " << clientKey; |
| 632 | } |
| 633 | else |
| 634 | { |
| 635 | BMCWEB_LOG_DEBUG << "Using existing connection pool for " |
| 636 | << clientKey; |
| 637 | } |
| 638 | |
| 639 | // Get the associated retry policy |
| 640 | auto policy = retryInfo.try_emplace(retryPolicyName); |
| 641 | if (policy.second) |
| 642 | { |
| 643 | BMCWEB_LOG_DEBUG << "Creating retry policy \"" << retryPolicyName |
| 644 | << "\" with default values"; |
| 645 | policy.first->second.name = retryPolicyName; |
| 646 | } |
| 647 | |
| 648 | // Send the data using either the existing connection pool or the newly |
| 649 | // created connection pool |
| 650 | result.first->second->sendData(data, policy.first->second); |
| 651 | } |
| 652 | |
| 653 | void setRetryConfig(const uint32_t retryAttempts, |
| 654 | const uint32_t retryTimeoutInterval, |
| 655 | const std::string& retryPolicyName) |
| 656 | { |
| 657 | // We need to create the retry policy if one does not already exist for |
| 658 | // the given retryPolicyName |
| 659 | auto result = retryInfo.try_emplace(retryPolicyName); |
| 660 | if (result.second) |
| 661 | { |
| 662 | BMCWEB_LOG_DEBUG << "setRetryConfig(): Creating new retry policy \"" |
| 663 | << retryPolicyName << "\""; |
| 664 | result.first->second.name = retryPolicyName; |
| 665 | } |
| 666 | else |
| 667 | { |
| 668 | BMCWEB_LOG_DEBUG << "setRetryConfig(): Updating retry info for \"" |
| 669 | << retryPolicyName << "\""; |
| 670 | } |
| 671 | |
| 672 | result.first->second.maxRetryAttempts = retryAttempts; |
| 673 | result.first->second.retryIntervalSecs = |
| 674 | std::chrono::seconds(retryTimeoutInterval); |
| 675 | } |
| 676 | |
| 677 | void setRetryPolicy(const std::string& retryPolicy, |
| 678 | const std::string& retryPolicyName) |
| 679 | { |
| 680 | // We need to create the retry policy if one does not already exist for |
| 681 | // the given retryPolicyName |
| 682 | auto result = retryInfo.try_emplace(retryPolicyName); |
| 683 | if (result.second) |
| 684 | { |
| 685 | BMCWEB_LOG_DEBUG << "setRetryPolicy(): Creating new retry policy \"" |
| 686 | << retryPolicyName << "\""; |
| 687 | result.first->second.name = retryPolicyName; |
| 688 | } |
| 689 | else |
| 690 | { |
| 691 | BMCWEB_LOG_DEBUG << "setRetryPolicy(): Updating retry policy for \"" |
| 692 | << retryPolicyName << "\""; |
| 693 | } |
| 694 | |
| 695 | result.first->second.retryPolicyAction = retryPolicy; |
| 696 | } |
| 697 | }; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 698 | } // namespace crow |