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