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