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 |
Nan Zhou | 77665bd | 2022-10-12 20:28:58 +0000 | [diff] [blame] | 17 | |
| 18 | #include "async_resolve.hpp" |
| 19 | #include "http_response.hpp" |
| 20 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 21 | #include <boost/asio/connect.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 22 | #include <boost/asio/io_context.hpp> |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 23 | #include <boost/asio/ip/address.hpp> |
| 24 | #include <boost/asio/ip/basic_endpoint.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 25 | #include <boost/asio/ip/tcp.hpp> |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 26 | #include <boost/asio/ssl/context.hpp> |
| 27 | #include <boost/asio/ssl/error.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 28 | #include <boost/asio/steady_timer.hpp> |
| 29 | #include <boost/beast/core/flat_buffer.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 30 | #include <boost/beast/core/flat_static_buffer.hpp> |
Ed Tanous | d43cd0c | 2020-09-30 20:46:53 -0700 | [diff] [blame] | 31 | #include <boost/beast/http/message.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 32 | #include <boost/beast/http/parser.hpp> |
| 33 | #include <boost/beast/http/read.hpp> |
| 34 | #include <boost/beast/http/string_body.hpp> |
| 35 | #include <boost/beast/http/write.hpp> |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 36 | #include <boost/beast/ssl/ssl_stream.hpp> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 37 | #include <boost/beast/version.hpp> |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 38 | #include <boost/container/devector.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 39 | #include <boost/system/error_code.hpp> |
Ed Tanous | bb49eb5 | 2022-06-28 12:02:42 -0700 | [diff] [blame] | 40 | #include <logging.hpp> |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 41 | #include <ssl_key_handler.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 42 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 43 | #include <cstdlib> |
| 44 | #include <functional> |
| 45 | #include <iostream> |
| 46 | #include <memory> |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 47 | #include <queue> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 48 | #include <string> |
| 49 | |
| 50 | namespace crow |
| 51 | { |
| 52 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 53 | // It is assumed that the BMC should be able to handle 4 parallel connections |
| 54 | constexpr uint8_t maxPoolSize = 4; |
| 55 | constexpr uint8_t maxRequestQueueSize = 50; |
Carson Labrado | 17dcc31 | 2022-07-28 22:17:28 +0000 | [diff] [blame] | 56 | constexpr unsigned int httpReadBodyLimit = 131072; |
Carson Labrado | 4d94272 | 2022-06-22 22:16:10 +0000 | [diff] [blame] | 57 | constexpr unsigned int httpReadBufferSize = 4096; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 58 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 59 | enum class ConnState |
| 60 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 61 | initialized, |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 62 | resolveInProgress, |
| 63 | resolveFailed, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 64 | connectInProgress, |
| 65 | connectFailed, |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 66 | connected, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 67 | handshakeInProgress, |
| 68 | handshakeFailed, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 69 | sendInProgress, |
| 70 | sendFailed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 71 | recvInProgress, |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 72 | recvFailed, |
| 73 | idle, |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 74 | closed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 75 | suspended, |
| 76 | terminated, |
| 77 | abortConnection, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 78 | sslInitFailed, |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 79 | retry |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 80 | }; |
| 81 | |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 82 | static inline boost::system::error_code |
| 83 | defaultRetryHandler(unsigned int respCode) |
| 84 | { |
| 85 | // As a default, assume 200X is alright |
| 86 | BMCWEB_LOG_DEBUG << "Using default check for response code validity"; |
| 87 | if ((respCode < 200) || (respCode >= 300)) |
| 88 | { |
| 89 | return boost::system::errc::make_error_code( |
| 90 | boost::system::errc::result_out_of_range); |
| 91 | } |
| 92 | |
| 93 | // Return 0 if the response code is valid |
| 94 | return boost::system::errc::make_error_code(boost::system::errc::success); |
| 95 | }; |
| 96 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 97 | // We need to allow retry information to be set before a message has been sent |
| 98 | // and a connection pool has been created |
| 99 | struct RetryPolicyData |
| 100 | { |
| 101 | uint32_t maxRetryAttempts = 5; |
| 102 | std::chrono::seconds retryIntervalSecs = std::chrono::seconds(0); |
| 103 | std::string retryPolicyAction = "TerminateAfterRetries"; |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 104 | std::function<boost::system::error_code(unsigned int respCode)> |
| 105 | invalidResp = defaultRetryHandler; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | struct PendingRequest |
| 109 | { |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 110 | boost::beast::http::request<boost::beast::http::string_body> req; |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 111 | std::function<void(bool, uint32_t, Response&)> callback; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 112 | RetryPolicyData retryPolicy; |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 113 | PendingRequest( |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 114 | boost::beast::http::request<boost::beast::http::string_body>&& reqIn, |
| 115 | const std::function<void(bool, uint32_t, Response&)>& callbackIn, |
| 116 | const RetryPolicyData& retryPolicyIn) : |
| 117 | req(std::move(reqIn)), |
| 118 | callback(callbackIn), retryPolicy(retryPolicyIn) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 119 | {} |
| 120 | }; |
| 121 | |
| 122 | class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo> |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 123 | { |
| 124 | private: |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 125 | ConnState state = ConnState::initialized; |
| 126 | uint32_t retryCount = 0; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 127 | std::string subId; |
| 128 | std::string host; |
| 129 | uint16_t port; |
| 130 | uint32_t connId; |
| 131 | |
| 132 | // Retry policy information |
| 133 | // This should be updated before each message is sent |
| 134 | RetryPolicyData retryPolicy; |
| 135 | |
| 136 | // Data buffers |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 137 | boost::beast::http::request<boost::beast::http::string_body> req; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 138 | std::optional< |
| 139 | boost::beast::http::response_parser<boost::beast::http::string_body>> |
| 140 | parser; |
Carson Labrado | 4d94272 | 2022-06-22 22:16:10 +0000 | [diff] [blame] | 141 | boost::beast::flat_static_buffer<httpReadBufferSize> buffer; |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 142 | Response res; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 143 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 144 | // Ascync callables |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 145 | std::function<void(bool, uint32_t, Response&)> callback; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 146 | crow::async_resolve::Resolver resolver; |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 147 | boost::asio::ip::tcp::socket conn; |
| 148 | std::optional<boost::beast::ssl_stream<boost::asio::ip::tcp::socket&>> |
| 149 | sslConn; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 150 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 151 | boost::asio::steady_timer timer; |
Ed Tanous | 84b3560 | 2021-09-08 20:06:32 -0700 | [diff] [blame] | 152 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 153 | friend class ConnectionPool; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 154 | |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 155 | void doResolve() |
| 156 | { |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 157 | state = ConnState::resolveInProgress; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 158 | BMCWEB_LOG_DEBUG << "Trying to resolve: " << host << ":" |
| 159 | << std::to_string(port) |
| 160 | << ", id: " << std::to_string(connId); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 161 | |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 162 | resolver.asyncResolve(host, port, |
| 163 | std::bind_front(&ConnectionInfo::afterResolve, |
| 164 | this, shared_from_this())); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 165 | } |
| 166 | |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 167 | void afterResolve( |
| 168 | const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 169 | const boost::beast::error_code ec, |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 170 | const std::vector<boost::asio::ip::tcp::endpoint>& endpointList) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 171 | { |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 172 | if (ec || (endpointList.empty())) |
| 173 | { |
| 174 | BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message(); |
| 175 | state = ConnState::resolveFailed; |
| 176 | waitAndRetry(); |
| 177 | return; |
| 178 | } |
| 179 | BMCWEB_LOG_DEBUG << "Resolved " << host << ":" << std::to_string(port) |
| 180 | << ", id: " << std::to_string(connId); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 181 | state = ConnState::connectInProgress; |
| 182 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 183 | BMCWEB_LOG_DEBUG << "Trying to connect to: " << host << ":" |
| 184 | << std::to_string(port) |
| 185 | << ", id: " << std::to_string(connId); |
Sunitha Harish | 29a82b0 | 2021-02-18 15:54:16 +0530 | [diff] [blame] | 186 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 187 | timer.expires_after(std::chrono::seconds(30)); |
| 188 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
| 189 | |
| 190 | boost::asio::async_connect( |
| 191 | conn, endpointList, |
| 192 | std::bind_front(&ConnectionInfo::afterConnect, this, |
| 193 | shared_from_this())); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void afterConnect(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 197 | boost::beast::error_code ec, |
| 198 | const boost::asio::ip::tcp::endpoint& endpoint) |
| 199 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 200 | // The operation already timed out. We don't want do continue down |
| 201 | // this branch |
| 202 | if (ec && ec == boost::asio::error::operation_aborted) |
| 203 | { |
| 204 | return; |
| 205 | } |
| 206 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 207 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 208 | if (ec) |
| 209 | { |
| 210 | BMCWEB_LOG_ERROR << "Connect " << endpoint.address().to_string() |
| 211 | << ":" << std::to_string(endpoint.port()) |
| 212 | << ", id: " << std::to_string(connId) |
| 213 | << " failed: " << ec.message(); |
| 214 | state = ConnState::connectFailed; |
| 215 | waitAndRetry(); |
| 216 | return; |
| 217 | } |
| 218 | BMCWEB_LOG_DEBUG << "Connected to: " << endpoint.address().to_string() |
| 219 | << ":" << std::to_string(endpoint.port()) |
| 220 | << ", id: " << std::to_string(connId); |
| 221 | if (sslConn) |
| 222 | { |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 223 | doSslHandshake(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | state = ConnState::connected; |
| 227 | sendMessage(); |
| 228 | } |
| 229 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 230 | void doSslHandshake() |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 231 | { |
| 232 | if (!sslConn) |
| 233 | { |
| 234 | return; |
| 235 | } |
| 236 | state = ConnState::handshakeInProgress; |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 237 | timer.expires_after(std::chrono::seconds(30)); |
| 238 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 239 | sslConn->async_handshake( |
| 240 | boost::asio::ssl::stream_base::client, |
| 241 | std::bind_front(&ConnectionInfo::afterSslHandshake, this, |
| 242 | shared_from_this())); |
| 243 | } |
| 244 | |
| 245 | void afterSslHandshake(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 246 | boost::beast::error_code ec) |
| 247 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 248 | // The operation already timed out. We don't want do continue down |
| 249 | // this branch |
| 250 | if (ec && ec == boost::asio::error::operation_aborted) |
| 251 | { |
| 252 | return; |
| 253 | } |
| 254 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 255 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 256 | if (ec) |
| 257 | { |
| 258 | BMCWEB_LOG_ERROR << "SSL Handshake failed -" |
| 259 | << " id: " << std::to_string(connId) |
| 260 | << " error: " << ec.message(); |
| 261 | state = ConnState::handshakeFailed; |
| 262 | waitAndRetry(); |
| 263 | return; |
| 264 | } |
| 265 | BMCWEB_LOG_DEBUG << "SSL Handshake successful -" |
| 266 | << " id: " << std::to_string(connId); |
| 267 | state = ConnState::connected; |
| 268 | sendMessage(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 269 | } |
| 270 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 271 | void sendMessage() |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 272 | { |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 273 | state = ConnState::sendInProgress; |
| 274 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 275 | // Set a timeout on the operation |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 276 | timer.expires_after(std::chrono::seconds(30)); |
| 277 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 278 | |
| 279 | // Send the HTTP request to the remote host |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 280 | if (sslConn) |
| 281 | { |
| 282 | boost::beast::http::async_write( |
| 283 | *sslConn, req, |
| 284 | std::bind_front(&ConnectionInfo::afterWrite, this, |
| 285 | shared_from_this())); |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | boost::beast::http::async_write( |
| 290 | conn, req, |
| 291 | std::bind_front(&ConnectionInfo::afterWrite, this, |
| 292 | shared_from_this())); |
| 293 | } |
| 294 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 295 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 296 | void afterWrite(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 297 | const boost::beast::error_code& ec, size_t bytesTransferred) |
| 298 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 299 | // The operation already timed out. We don't want do continue down |
| 300 | // this branch |
| 301 | if (ec && ec == boost::asio::error::operation_aborted) |
| 302 | { |
| 303 | return; |
| 304 | } |
| 305 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 306 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 307 | if (ec) |
| 308 | { |
| 309 | BMCWEB_LOG_ERROR << "sendMessage() failed: " << ec.message(); |
| 310 | state = ConnState::sendFailed; |
| 311 | waitAndRetry(); |
| 312 | return; |
| 313 | } |
| 314 | BMCWEB_LOG_DEBUG << "sendMessage() bytes transferred: " |
| 315 | << bytesTransferred; |
| 316 | |
| 317 | recvMessage(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void recvMessage() |
| 321 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 322 | state = ConnState::recvInProgress; |
| 323 | |
| 324 | parser.emplace(std::piecewise_construct, std::make_tuple()); |
| 325 | parser->body_limit(httpReadBodyLimit); |
| 326 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 327 | timer.expires_after(std::chrono::seconds(30)); |
| 328 | timer.async_wait(std::bind_front(onTimeout, weak_from_this())); |
| 329 | |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 330 | // Receive the HTTP response |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 331 | if (sslConn) |
| 332 | { |
| 333 | boost::beast::http::async_read( |
| 334 | *sslConn, buffer, *parser, |
| 335 | std::bind_front(&ConnectionInfo::afterRead, this, |
| 336 | shared_from_this())); |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | boost::beast::http::async_read( |
| 341 | conn, buffer, *parser, |
| 342 | std::bind_front(&ConnectionInfo::afterRead, this, |
| 343 | shared_from_this())); |
| 344 | } |
| 345 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 346 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 347 | void afterRead(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 348 | const boost::beast::error_code& ec, |
| 349 | const std::size_t& bytesTransferred) |
| 350 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 351 | // The operation already timed out. We don't want do continue down |
| 352 | // this branch |
| 353 | if (ec && ec == boost::asio::error::operation_aborted) |
| 354 | { |
| 355 | return; |
| 356 | } |
| 357 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 358 | timer.cancel(); |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 359 | if (ec && ec != boost::asio::ssl::error::stream_truncated) |
| 360 | { |
| 361 | BMCWEB_LOG_ERROR << "recvMessage() failed: " << ec.message(); |
| 362 | state = ConnState::recvFailed; |
| 363 | waitAndRetry(); |
| 364 | return; |
| 365 | } |
| 366 | BMCWEB_LOG_DEBUG << "recvMessage() bytes transferred: " |
| 367 | << bytesTransferred; |
| 368 | BMCWEB_LOG_DEBUG << "recvMessage() data: " << parser->get().body(); |
| 369 | |
| 370 | unsigned int respCode = parser->get().result_int(); |
| 371 | BMCWEB_LOG_DEBUG << "recvMessage() Header Response Code: " << respCode; |
| 372 | |
| 373 | // Make sure the received response code is valid as defined by |
| 374 | // the associated retry policy |
| 375 | if (retryPolicy.invalidResp(respCode)) |
| 376 | { |
| 377 | // The listener failed to receive the Sent-Event |
| 378 | BMCWEB_LOG_ERROR << "recvMessage() Listener Failed to " |
| 379 | "receive Sent-Event. Header Response Code: " |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 380 | << respCode; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 381 | state = ConnState::recvFailed; |
| 382 | waitAndRetry(); |
| 383 | return; |
| 384 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 385 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 386 | // Send is successful |
| 387 | // Reset the counter just in case this was after retrying |
| 388 | retryCount = 0; |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 389 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 390 | // Keep the connection alive if server supports it |
| 391 | // Else close the connection |
| 392 | BMCWEB_LOG_DEBUG << "recvMessage() keepalive : " |
| 393 | << parser->keep_alive(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 394 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 395 | // Copy the response into a Response object so that it can be |
| 396 | // processed by the callback function. |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 397 | res.stringResponse = parser->release(); |
| 398 | callback(parser->keep_alive(), connId, res); |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 399 | res.clear(); |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 400 | } |
| 401 | |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 402 | static void onTimeout(const std::weak_ptr<ConnectionInfo>& weakSelf, |
| 403 | const boost::system::error_code ec) |
| 404 | { |
| 405 | if (ec == boost::asio::error::operation_aborted) |
| 406 | { |
| 407 | BMCWEB_LOG_DEBUG |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 408 | << "async_wait failed since the operation is aborted"; |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 409 | return; |
| 410 | } |
| 411 | if (ec) |
| 412 | { |
| 413 | BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message(); |
| 414 | // If the timer fails, we need to close the socket anyway, same as |
| 415 | // if it expired. |
| 416 | } |
| 417 | std::shared_ptr<ConnectionInfo> self = weakSelf.lock(); |
| 418 | if (self == nullptr) |
| 419 | { |
| 420 | return; |
| 421 | } |
| 422 | self->waitAndRetry(); |
| 423 | } |
| 424 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 425 | void waitAndRetry() |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 426 | { |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 427 | if ((retryCount >= retryPolicy.maxRetryAttempts) || |
| 428 | (state == ConnState::sslInitFailed)) |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 429 | { |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 430 | BMCWEB_LOG_ERROR << "Maximum number of retries reached."; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 431 | BMCWEB_LOG_DEBUG << "Retry policy: " |
| 432 | << retryPolicy.retryPolicyAction; |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 433 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 434 | if (retryPolicy.retryPolicyAction == "TerminateAfterRetries") |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 435 | { |
| 436 | // TODO: delete subscription |
| 437 | state = ConnState::terminated; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 438 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 439 | if (retryPolicy.retryPolicyAction == "SuspendRetries") |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 440 | { |
| 441 | state = ConnState::suspended; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 442 | } |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 443 | |
| 444 | // We want to return a 502 to indicate there was an error with |
| 445 | // the external server |
| 446 | res.result(boost::beast::http::status::bad_gateway); |
| 447 | callback(false, connId, res); |
| 448 | res.clear(); |
| 449 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 450 | // Reset the retrycount to zero so that client can try connecting |
| 451 | // again if needed |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 452 | retryCount = 0; |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 453 | return; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 454 | } |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 455 | |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 456 | retryCount++; |
| 457 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 458 | BMCWEB_LOG_DEBUG << "Attempt retry after " |
| 459 | << std::to_string( |
| 460 | retryPolicy.retryIntervalSecs.count()) |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 461 | << " seconds. RetryCount = " << retryCount; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 462 | timer.expires_after(retryPolicy.retryIntervalSecs); |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 463 | timer.async_wait(std::bind_front(&ConnectionInfo::onTimerDone, this, |
| 464 | shared_from_this())); |
| 465 | } |
Sunitha Harish | 6eaa1d2 | 2021-02-19 13:38:31 +0530 | [diff] [blame] | 466 | |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 467 | void onTimerDone(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 468 | const boost::system::error_code& ec) |
| 469 | { |
| 470 | if (ec == boost::asio::error::operation_aborted) |
| 471 | { |
| 472 | BMCWEB_LOG_DEBUG |
| 473 | << "async_wait failed since the operation is aborted" |
| 474 | << ec.message(); |
| 475 | } |
| 476 | else if (ec) |
| 477 | { |
| 478 | BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message(); |
| 479 | // Ignore the error and continue the retry loop to attempt |
| 480 | // sending the event as per the retry policy |
| 481 | } |
| 482 | |
| 483 | // Let's close the connection and restart from resolve. |
| 484 | doClose(true); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 485 | } |
| 486 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 487 | void shutdownConn(bool retry) |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 488 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 489 | boost::beast::error_code ec; |
Ed Tanous | 0d5f5cf | 2022-03-12 15:30:55 -0800 | [diff] [blame] | 490 | conn.shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 491 | conn.close(); |
| 492 | |
| 493 | // not_connected happens sometimes so don't bother reporting it. |
| 494 | if (ec && ec != boost::beast::errc::not_connected) |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 495 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 496 | BMCWEB_LOG_ERROR << host << ":" << std::to_string(port) |
| 497 | << ", id: " << std::to_string(connId) |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 498 | << " shutdown failed: " << ec.message(); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 499 | } |
Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 500 | else |
| 501 | { |
| 502 | BMCWEB_LOG_DEBUG << host << ":" << std::to_string(port) |
| 503 | << ", id: " << std::to_string(connId) |
| 504 | << " closed gracefully"; |
| 505 | } |
Ed Tanous | ca72376 | 2022-06-28 19:40:39 -0700 | [diff] [blame] | 506 | |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 507 | if (retry) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 508 | { |
Carson Labrado | 513d1ff | 2022-07-19 00:38:15 +0000 | [diff] [blame] | 509 | // Now let's try to resend the data |
| 510 | state = ConnState::retry; |
| 511 | doResolve(); |
| 512 | } |
| 513 | else |
| 514 | { |
| 515 | state = ConnState::closed; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 516 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 517 | } |
| 518 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 519 | void doClose(bool retry = false) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 520 | { |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 521 | if (!sslConn) |
| 522 | { |
| 523 | shutdownConn(retry); |
| 524 | return; |
| 525 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 526 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 527 | sslConn->async_shutdown( |
| 528 | std::bind_front(&ConnectionInfo::afterSslShutdown, this, |
| 529 | shared_from_this(), retry)); |
| 530 | } |
| 531 | |
| 532 | void afterSslShutdown(const std::shared_ptr<ConnectionInfo>& /*self*/, |
| 533 | bool retry, const boost::system::error_code& ec) |
| 534 | { |
| 535 | |
| 536 | if (ec) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 537 | { |
| 538 | BMCWEB_LOG_ERROR << host << ":" << std::to_string(port) |
| 539 | << ", id: " << std::to_string(connId) |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 540 | << " shutdown failed: " << ec.message(); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 541 | } |
Carson Labrado | 5cab68f | 2022-07-11 22:26:21 +0000 | [diff] [blame] | 542 | else |
| 543 | { |
| 544 | BMCWEB_LOG_DEBUG << host << ":" << std::to_string(port) |
| 545 | << ", id: " << std::to_string(connId) |
| 546 | << " closed gracefully"; |
| 547 | } |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 548 | shutdownConn(retry); |
| 549 | } |
Ed Tanous | ca72376 | 2022-06-28 19:40:39 -0700 | [diff] [blame] | 550 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 551 | void setCipherSuiteTLSext() |
| 552 | { |
| 553 | if (!sslConn) |
| 554 | { |
| 555 | return; |
| 556 | } |
| 557 | // NOTE: The SSL_set_tlsext_host_name is defined in tlsv1.h header |
| 558 | // file but its having old style casting (name is cast to void*). |
| 559 | // Since bmcweb compiler treats all old-style-cast as error, its |
| 560 | // causing the build failure. So replaced the same macro inline and |
| 561 | // did corrected the code by doing static_cast to viod*. This has to |
| 562 | // be fixed in openssl library in long run. Set SNI Hostname (many |
| 563 | // hosts need this to handshake successfully) |
| 564 | if (SSL_ctrl(sslConn->native_handle(), SSL_CTRL_SET_TLSEXT_HOSTNAME, |
| 565 | TLSEXT_NAMETYPE_host_name, |
| 566 | static_cast<void*>(&host.front())) == 0) |
| 567 | |
| 568 | { |
| 569 | boost::beast::error_code ec{static_cast<int>(::ERR_get_error()), |
| 570 | boost::asio::error::get_ssl_category()}; |
| 571 | |
| 572 | BMCWEB_LOG_ERROR << "SSL_set_tlsext_host_name " << host << ":" |
| 573 | << port << ", id: " << std::to_string(connId) |
| 574 | << " failed: " << ec.message(); |
| 575 | // Set state as sslInit failed so that we close the connection |
| 576 | // and take appropriate action as per retry configuration. |
| 577 | state = ConnState::sslInitFailed; |
| 578 | waitAndRetry(); |
| 579 | return; |
| 580 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | public: |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 584 | explicit ConnectionInfo(boost::asio::io_context& iocIn, |
| 585 | const std::string& idIn, |
| 586 | const std::string& destIPIn, uint16_t destPortIn, |
| 587 | bool useSSL, unsigned int connIdIn) : |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 588 | subId(idIn), |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 589 | host(destIPIn), port(destPortIn), connId(connIdIn), conn(iocIn), |
| 590 | timer(iocIn) |
| 591 | { |
| 592 | if (useSSL) |
| 593 | { |
| 594 | std::optional<boost::asio::ssl::context> sslCtx = |
| 595 | ensuressl::getSSLClientContext(); |
| 596 | |
| 597 | if (!sslCtx) |
| 598 | { |
| 599 | BMCWEB_LOG_ERROR << "prepareSSLContext failed - " << host << ":" |
| 600 | << port << ", id: " << std::to_string(connId); |
| 601 | // Don't retry if failure occurs while preparing SSL context |
| 602 | // such as certificate is invalid or set cipher failure or set |
| 603 | // host name failure etc... Setting conn state to sslInitFailed |
| 604 | // and connection state will be transitioned to next state |
| 605 | // depending on retry policy set by subscription. |
| 606 | state = ConnState::sslInitFailed; |
| 607 | waitAndRetry(); |
| 608 | return; |
| 609 | } |
| 610 | sslConn.emplace(conn, *sslCtx); |
| 611 | setCipherSuiteTLSext(); |
| 612 | } |
| 613 | } |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 614 | }; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 615 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 616 | class ConnectionPool : public std::enable_shared_from_this<ConnectionPool> |
| 617 | { |
| 618 | private: |
| 619 | boost::asio::io_context& ioc; |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 620 | std::string id; |
| 621 | std::string destIP; |
| 622 | uint16_t destPort; |
| 623 | bool useSSL; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 624 | std::vector<std::shared_ptr<ConnectionInfo>> connections; |
| 625 | boost::container::devector<PendingRequest> requestQueue; |
| 626 | |
| 627 | friend class HttpClient; |
| 628 | |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 629 | // Configure a connections's request, callback, and retry info in |
| 630 | // preparation to begin sending the request |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 631 | void setConnProps(ConnectionInfo& conn) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 632 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 633 | if (requestQueue.empty()) |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 634 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 635 | BMCWEB_LOG_ERROR |
| 636 | << "setConnProps() should not have been called when requestQueue is empty"; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 637 | return; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 638 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 639 | |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 640 | auto nextReq = requestQueue.front(); |
| 641 | conn.retryPolicy = std::move(nextReq.retryPolicy); |
| 642 | conn.req = std::move(nextReq.req); |
| 643 | conn.callback = std::move(nextReq.callback); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 644 | |
| 645 | BMCWEB_LOG_DEBUG << "Setting properties for connection " << conn.host |
| 646 | << ":" << std::to_string(conn.port) |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 647 | << ", id: " << std::to_string(conn.connId); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 648 | |
| 649 | // We can remove the request from the queue at this point |
| 650 | requestQueue.pop_front(); |
| 651 | } |
| 652 | |
| 653 | // Configures a connection to use the specific retry policy. |
| 654 | inline void setConnRetryPolicy(ConnectionInfo& conn, |
| 655 | const RetryPolicyData& retryPolicy) |
| 656 | { |
| 657 | BMCWEB_LOG_DEBUG << destIP << ":" << std::to_string(destPort) |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 658 | << ", id: " << std::to_string(conn.connId); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 659 | |
| 660 | conn.retryPolicy = retryPolicy; |
| 661 | } |
| 662 | |
| 663 | // Gets called as part of callback after request is sent |
| 664 | // Reuses the connection if there are any requests waiting to be sent |
| 665 | // Otherwise closes the connection if it is not a keep-alive |
| 666 | void sendNext(bool keepAlive, uint32_t connId) |
| 667 | { |
| 668 | auto conn = connections[connId]; |
Carson Labrado | 46a8146 | 2022-04-27 21:11:37 +0000 | [diff] [blame] | 669 | |
| 670 | // Allow the connection's handler to be deleted |
| 671 | // This is needed because of Redfish Aggregation passing an |
| 672 | // AsyncResponse shared_ptr to this callback |
| 673 | conn->callback = nullptr; |
| 674 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 675 | // Reuse the connection to send the next request in the queue |
| 676 | if (!requestQueue.empty()) |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 677 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 678 | BMCWEB_LOG_DEBUG << std::to_string(requestQueue.size()) |
| 679 | << " requests remaining in queue for " << destIP |
| 680 | << ":" << std::to_string(destPort) |
| 681 | << ", reusing connnection " |
| 682 | << std::to_string(connId); |
| 683 | |
| 684 | setConnProps(*conn); |
| 685 | |
| 686 | if (keepAlive) |
| 687 | { |
| 688 | conn->sendMessage(); |
| 689 | } |
| 690 | else |
| 691 | { |
| 692 | // Server is not keep-alive enabled so we need to close the |
| 693 | // connection and then start over from resolve |
| 694 | conn->doClose(); |
| 695 | conn->doResolve(); |
| 696 | } |
| 697 | return; |
| 698 | } |
| 699 | |
| 700 | // No more messages to send so close the connection if necessary |
| 701 | if (keepAlive) |
| 702 | { |
| 703 | conn->state = ConnState::idle; |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 704 | } |
| 705 | else |
| 706 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 707 | // Abort the connection since server is not keep-alive enabled |
| 708 | conn->state = ConnState::abortConnection; |
| 709 | conn->doClose(); |
AppaRao Puli | 2a5689a | 2020-04-29 15:24:31 +0530 | [diff] [blame] | 710 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 711 | } |
| 712 | |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 713 | void sendData(std::string& data, const std::string& destUri, |
| 714 | const boost::beast::http::fields& httpHeader, |
| 715 | const boost::beast::http::verb verb, |
| 716 | const RetryPolicyData& retryPolicy, |
Ed Tanous | 6b3db60 | 2022-06-28 19:41:44 -0700 | [diff] [blame] | 717 | const std::function<void(Response&)>& resHandler) |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 718 | { |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 719 | // Construct the request to be sent |
| 720 | boost::beast::http::request<boost::beast::http::string_body> thisReq( |
| 721 | verb, destUri, 11, "", httpHeader); |
| 722 | thisReq.set(boost::beast::http::field::host, destIP); |
| 723 | thisReq.keep_alive(true); |
| 724 | thisReq.body() = std::move(data); |
| 725 | thisReq.prepare_payload(); |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 726 | auto cb = std::bind_front(&ConnectionPool::afterSendData, |
| 727 | weak_from_this(), resHandler); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 728 | // Reuse an existing connection if one is available |
| 729 | for (unsigned int i = 0; i < connections.size(); i++) |
| 730 | { |
| 731 | auto conn = connections[i]; |
| 732 | if ((conn->state == ConnState::idle) || |
| 733 | (conn->state == ConnState::initialized) || |
| 734 | (conn->state == ConnState::closed)) |
| 735 | { |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 736 | conn->req = std::move(thisReq); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 737 | conn->callback = std::move(cb); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 738 | setConnRetryPolicy(*conn, retryPolicy); |
| 739 | std::string commonMsg = std::to_string(i) + " from pool " + |
| 740 | destIP + ":" + std::to_string(destPort); |
| 741 | |
| 742 | if (conn->state == ConnState::idle) |
| 743 | { |
| 744 | BMCWEB_LOG_DEBUG << "Grabbing idle connection " |
| 745 | << commonMsg; |
| 746 | conn->sendMessage(); |
| 747 | } |
| 748 | else |
| 749 | { |
| 750 | BMCWEB_LOG_DEBUG << "Reusing existing connection " |
| 751 | << commonMsg; |
| 752 | conn->doResolve(); |
| 753 | } |
| 754 | return; |
| 755 | } |
| 756 | } |
| 757 | |
| 758 | // All connections in use so create a new connection or add request to |
| 759 | // the queue |
| 760 | if (connections.size() < maxPoolSize) |
| 761 | { |
| 762 | BMCWEB_LOG_DEBUG << "Adding new connection to pool " << destIP |
| 763 | << ":" << std::to_string(destPort); |
| 764 | auto conn = addConnection(); |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 765 | conn->req = std::move(thisReq); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 766 | conn->callback = std::move(cb); |
| 767 | setConnRetryPolicy(*conn, retryPolicy); |
| 768 | conn->doResolve(); |
| 769 | } |
| 770 | else if (requestQueue.size() < maxRequestQueueSize) |
| 771 | { |
| 772 | BMCWEB_LOG_ERROR << "Max pool size reached. Adding data to queue."; |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 773 | requestQueue.emplace_back(std::move(thisReq), std::move(cb), |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 774 | retryPolicy); |
| 775 | } |
| 776 | else |
| 777 | { |
| 778 | BMCWEB_LOG_ERROR << destIP << ":" << std::to_string(destPort) |
| 779 | << " request queue full. Dropping request."; |
| 780 | } |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 781 | } |
| 782 | |
Ed Tanous | 3d36e3a | 2022-08-19 15:54:04 -0700 | [diff] [blame] | 783 | // Callback to be called once the request has been sent |
| 784 | static void afterSendData(const std::weak_ptr<ConnectionPool>& weakSelf, |
| 785 | const std::function<void(Response&)>& resHandler, |
| 786 | bool keepAlive, uint32_t connId, Response& res) |
| 787 | { |
| 788 | // Allow provided callback to perform additional processing of the |
| 789 | // request |
| 790 | resHandler(res); |
| 791 | |
| 792 | // If requests remain in the queue then we want to reuse this |
| 793 | // connection to send the next request |
| 794 | std::shared_ptr<ConnectionPool> self = weakSelf.lock(); |
| 795 | if (!self) |
| 796 | { |
| 797 | BMCWEB_LOG_CRITICAL << self << " Failed to capture connection"; |
| 798 | return; |
| 799 | } |
| 800 | |
| 801 | self->sendNext(keepAlive, connId); |
| 802 | } |
| 803 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 804 | std::shared_ptr<ConnectionInfo>& addConnection() |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 805 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 806 | unsigned int newId = static_cast<unsigned int>(connections.size()); |
| 807 | |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 808 | auto& ret = connections.emplace_back(std::make_shared<ConnectionInfo>( |
| 809 | ioc, id, destIP, destPort, useSSL, newId)); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 810 | |
| 811 | BMCWEB_LOG_DEBUG << "Added connection " |
| 812 | << std::to_string(connections.size() - 1) |
| 813 | << " to pool " << destIP << ":" |
| 814 | << std::to_string(destPort); |
| 815 | |
| 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | public: |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 820 | explicit ConnectionPool(boost::asio::io_context& iocIn, |
| 821 | const std::string& idIn, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 822 | const std::string& destIPIn, uint16_t destPortIn, |
| 823 | bool useSSLIn) : |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 824 | ioc(iocIn), |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 825 | id(idIn), destIP(destIPIn), destPort(destPortIn), useSSL(useSSLIn) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 826 | { |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 827 | BMCWEB_LOG_DEBUG << "Initializing connection pool for " << destIP << ":" |
| 828 | << std::to_string(destPort); |
| 829 | |
| 830 | // Initialize the pool with a single connection |
| 831 | addConnection(); |
Ayushi Smriti | fe44eb0 | 2020-05-15 15:24:45 +0530 | [diff] [blame] | 832 | } |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 833 | }; |
| 834 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 835 | class HttpClient |
| 836 | { |
| 837 | private: |
| 838 | std::unordered_map<std::string, std::shared_ptr<ConnectionPool>> |
| 839 | connectionPools; |
| 840 | boost::asio::io_context& ioc = |
| 841 | crow::connections::systemBus->get_io_context(); |
| 842 | std::unordered_map<std::string, RetryPolicyData> retryInfo; |
| 843 | HttpClient() = default; |
| 844 | |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 845 | // Used as a dummy callback by sendData() in order to call |
| 846 | // sendDataWithCallback() |
Ed Tanous | 02cad96 | 2022-06-30 16:50:15 -0700 | [diff] [blame] | 847 | static void genericResHandler(const Response& res) |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 848 | { |
| 849 | BMCWEB_LOG_DEBUG << "Response handled with return code: " |
| 850 | << std::to_string(res.resultInt()); |
Ed Tanous | 4ee8e21 | 2022-05-28 09:42:51 -0700 | [diff] [blame] | 851 | } |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 852 | |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 853 | public: |
| 854 | HttpClient(const HttpClient&) = delete; |
| 855 | HttpClient& operator=(const HttpClient&) = delete; |
| 856 | HttpClient(HttpClient&&) = delete; |
| 857 | HttpClient& operator=(HttpClient&&) = delete; |
| 858 | ~HttpClient() = default; |
| 859 | |
| 860 | static HttpClient& getInstance() |
| 861 | { |
| 862 | static HttpClient handler; |
| 863 | return handler; |
| 864 | } |
| 865 | |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 866 | // Send a request to destIP:destPort where additional processing of the |
| 867 | // result is not required |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 868 | void sendData(std::string& data, const std::string& id, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 869 | const std::string& destIP, uint16_t destPort, |
| 870 | const std::string& destUri, bool useSSL, |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 871 | const boost::beast::http::fields& httpHeader, |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 872 | const boost::beast::http::verb verb, |
| 873 | const std::string& retryPolicyName) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 874 | { |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 875 | const std::function<void(Response&)> cb = genericResHandler; |
| 876 | sendDataWithCallback(data, id, destIP, destPort, destUri, useSSL, |
| 877 | httpHeader, verb, retryPolicyName, cb); |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | // Send request to destIP:destPort and use the provided callback to |
| 881 | // handle the response |
| 882 | void sendDataWithCallback(std::string& data, const std::string& id, |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 883 | const std::string& destIP, uint16_t destPort, |
| 884 | const std::string& destUri, bool useSSL, |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 885 | const boost::beast::http::fields& httpHeader, |
Carson Labrado | 244256c | 2022-04-27 17:16:32 +0000 | [diff] [blame] | 886 | const boost::beast::http::verb verb, |
| 887 | const std::string& retryPolicyName, |
Ed Tanous | 6b3db60 | 2022-06-28 19:41:44 -0700 | [diff] [blame] | 888 | const std::function<void(Response&)>& resHandler) |
Carson Labrado | 039a47e | 2022-04-05 16:03:20 +0000 | [diff] [blame] | 889 | { |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 890 | std::string clientKey = useSSL ? "https" : "http"; |
| 891 | clientKey += destIP; |
| 892 | clientKey += ":"; |
| 893 | clientKey += std::to_string(destPort); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 894 | // Use nullptr to avoid creating a ConnectionPool each time |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 895 | std::shared_ptr<ConnectionPool>& conn = connectionPools[clientKey]; |
| 896 | if (conn == nullptr) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 897 | { |
| 898 | // Now actually create the ConnectionPool shared_ptr since it does |
| 899 | // not already exist |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 900 | conn = std::make_shared<ConnectionPool>(ioc, id, destIP, destPort, |
| 901 | useSSL); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 902 | BMCWEB_LOG_DEBUG << "Created connection pool for " << clientKey; |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | BMCWEB_LOG_DEBUG << "Using existing connection pool for " |
| 907 | << clientKey; |
| 908 | } |
| 909 | |
| 910 | // Get the associated retry policy |
| 911 | auto policy = retryInfo.try_emplace(retryPolicyName); |
| 912 | if (policy.second) |
| 913 | { |
| 914 | BMCWEB_LOG_DEBUG << "Creating retry policy \"" << retryPolicyName |
| 915 | << "\" with default values"; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | // Send the data using either the existing connection pool or the newly |
| 919 | // created connection pool |
AppaRao Puli | e38778a | 2022-06-27 23:09:03 +0000 | [diff] [blame] | 920 | conn->sendData(data, destUri, httpHeader, verb, policy.first->second, |
| 921 | resHandler); |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 924 | void setRetryConfig( |
| 925 | const uint32_t retryAttempts, const uint32_t retryTimeoutInterval, |
| 926 | const std::function<boost::system::error_code(unsigned int respCode)>& |
| 927 | invalidResp, |
| 928 | const std::string& retryPolicyName) |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 929 | { |
| 930 | // We need to create the retry policy if one does not already exist for |
| 931 | // the given retryPolicyName |
| 932 | auto result = retryInfo.try_emplace(retryPolicyName); |
| 933 | if (result.second) |
| 934 | { |
| 935 | BMCWEB_LOG_DEBUG << "setRetryConfig(): Creating new retry policy \"" |
| 936 | << retryPolicyName << "\""; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 937 | } |
| 938 | else |
| 939 | { |
| 940 | BMCWEB_LOG_DEBUG << "setRetryConfig(): Updating retry info for \"" |
| 941 | << retryPolicyName << "\""; |
| 942 | } |
| 943 | |
| 944 | result.first->second.maxRetryAttempts = retryAttempts; |
| 945 | result.first->second.retryIntervalSecs = |
| 946 | std::chrono::seconds(retryTimeoutInterval); |
Carson Labrado | a7a8029 | 2022-06-01 16:01:52 +0000 | [diff] [blame] | 947 | result.first->second.invalidResp = invalidResp; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | void setRetryPolicy(const std::string& retryPolicy, |
| 951 | const std::string& retryPolicyName) |
| 952 | { |
| 953 | // We need to create the retry policy if one does not already exist for |
| 954 | // the given retryPolicyName |
| 955 | auto result = retryInfo.try_emplace(retryPolicyName); |
| 956 | if (result.second) |
| 957 | { |
| 958 | BMCWEB_LOG_DEBUG << "setRetryPolicy(): Creating new retry policy \"" |
| 959 | << retryPolicyName << "\""; |
Carson Labrado | f52c03c | 2022-03-23 18:50:15 +0000 | [diff] [blame] | 960 | } |
| 961 | else |
| 962 | { |
| 963 | BMCWEB_LOG_DEBUG << "setRetryPolicy(): Updating retry policy for \"" |
| 964 | << retryPolicyName << "\""; |
| 965 | } |
| 966 | |
| 967 | result.first->second.retryPolicyAction = retryPolicy; |
| 968 | } |
| 969 | }; |
AppaRao Puli | bd030d0 | 2020-03-20 03:34:29 +0530 | [diff] [blame] | 970 | } // namespace crow |