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