blob: 06d4a1ca6420b85af276929b23a0dd4f6b93db24 [file] [log] [blame]
AppaRao Pulibd030d02020-03-20 03:34:29 +05301/*
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 Harish29a82b02021-02-18 15:54:16 +053017#include <boost/asio/ip/address.hpp>
18#include <boost/asio/ip/basic_endpoint.hpp>
Ed Tanousd43cd0c2020-09-30 20:46:53 -070019#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 Pulibd030d02020-03-20 03:34:29 +053023#include <boost/beast/version.hpp>
Carson Labradof52c03c2022-03-23 18:50:15 +000024#include <boost/container/devector.hpp>
Sunitha Harish29a82b02021-02-18 15:54:16 +053025#include <include/async_resolve.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050026
AppaRao Pulibd030d02020-03-20 03:34:29 +053027#include <cstdlib>
28#include <functional>
29#include <iostream>
30#include <memory>
AppaRao Puli2a5689a2020-04-29 15:24:31 +053031#include <queue>
AppaRao Pulibd030d02020-03-20 03:34:29 +053032#include <string>
33
34namespace crow
35{
36
Carson Labradof52c03c2022-03-23 18:50:15 +000037// It is assumed that the BMC should be able to handle 4 parallel connections
38constexpr uint8_t maxPoolSize = 4;
39constexpr uint8_t maxRequestQueueSize = 50;
40constexpr unsigned int httpReadBodyLimit = 8192;
AppaRao Puli2a5689a2020-04-29 15:24:31 +053041
AppaRao Pulibd030d02020-03-20 03:34:29 +053042enum class ConnState
43{
AppaRao Puli2a5689a2020-04-29 15:24:31 +053044 initialized,
Sunitha Harish29a82b02021-02-18 15:54:16 +053045 resolveInProgress,
46 resolveFailed,
AppaRao Puli2a5689a2020-04-29 15:24:31 +053047 connectInProgress,
48 connectFailed,
AppaRao Pulibd030d02020-03-20 03:34:29 +053049 connected,
AppaRao Puli2a5689a2020-04-29 15:24:31 +053050 sendInProgress,
51 sendFailed,
Sunitha Harish6eaa1d22021-02-19 13:38:31 +053052 recvInProgress,
AppaRao Puli2a5689a2020-04-29 15:24:31 +053053 recvFailed,
54 idle,
Sunitha Harish6eaa1d22021-02-19 13:38:31 +053055 closeInProgress,
Ayushi Smritife44eb02020-05-15 15:24:45 +053056 closed,
Sunitha Harish6eaa1d22021-02-19 13:38:31 +053057 suspended,
58 terminated,
59 abortConnection,
60 retry
AppaRao Pulibd030d02020-03-20 03:34:29 +053061};
62
Carson Labradof52c03c2022-03-23 18:50:15 +000063// We need to allow retry information to be set before a message has been sent
64// and a connection pool has been created
65struct RetryPolicyData
66{
67 uint32_t maxRetryAttempts = 5;
68 std::chrono::seconds retryIntervalSecs = std::chrono::seconds(0);
69 std::string retryPolicyAction = "TerminateAfterRetries";
70 std::string name;
71};
72
73struct PendingRequest
74{
Carson Labrado244256c2022-04-27 17:16:32 +000075 boost::beast::http::request<boost::beast::http::string_body> req;
Carson Labrado039a47e2022-04-05 16:03:20 +000076 std::function<void(bool, uint32_t, Response&)> callback;
Carson Labradof52c03c2022-03-23 18:50:15 +000077 RetryPolicyData retryPolicy;
Carson Labrado039a47e2022-04-05 16:03:20 +000078 PendingRequest(
Carson Labrado244256c2022-04-27 17:16:32 +000079 boost::beast::http::request<boost::beast::http::string_body>&& req,
Carson Labrado039a47e2022-04-05 16:03:20 +000080 const std::function<void(bool, uint32_t, Response&)>& callback,
81 const RetryPolicyData& retryPolicy) :
Carson Labrado244256c2022-04-27 17:16:32 +000082 req(std::move(req)),
Carson Labradof52c03c2022-03-23 18:50:15 +000083 callback(callback), retryPolicy(retryPolicy)
84 {}
85};
86
87class ConnectionInfo : public std::enable_shared_from_this<ConnectionInfo>
AppaRao Pulibd030d02020-03-20 03:34:29 +053088{
89 private:
Carson Labradof52c03c2022-03-23 18:50:15 +000090 ConnState state = ConnState::initialized;
91 uint32_t retryCount = 0;
92 bool runningTimer = false;
93 std::string subId;
94 std::string host;
95 uint16_t port;
96 uint32_t connId;
97
98 // Retry policy information
99 // This should be updated before each message is sent
100 RetryPolicyData retryPolicy;
101
102 // Data buffers
AppaRao Pulibd030d02020-03-20 03:34:29 +0530103 boost::beast::http::request<boost::beast::http::string_body> req;
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530104 std::optional<
105 boost::beast::http::response_parser<boost::beast::http::string_body>>
106 parser;
Carson Labradof52c03c2022-03-23 18:50:15 +0000107 boost::beast::flat_static_buffer<httpReadBodyLimit> buffer;
Carson Labrado039a47e2022-04-05 16:03:20 +0000108 Response res;
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530109
Carson Labradof52c03c2022-03-23 18:50:15 +0000110 // Ascync callables
Carson Labrado039a47e2022-04-05 16:03:20 +0000111 std::function<void(bool, uint32_t, Response&)> callback;
Carson Labradof52c03c2022-03-23 18:50:15 +0000112 crow::async_resolve::Resolver resolver;
113 boost::beast::tcp_stream conn;
114 boost::asio::steady_timer timer;
Ed Tanous84b35602021-09-08 20:06:32 -0700115
Carson Labradof52c03c2022-03-23 18:50:15 +0000116 friend class ConnectionPool;
AppaRao Pulibd030d02020-03-20 03:34:29 +0530117
Sunitha Harish29a82b02021-02-18 15:54:16 +0530118 void doResolve()
119 {
Sunitha Harish29a82b02021-02-18 15:54:16 +0530120 state = ConnState::resolveInProgress;
Carson Labradof52c03c2022-03-23 18:50:15 +0000121 BMCWEB_LOG_DEBUG << "Trying to resolve: " << host << ":"
122 << std::to_string(port)
123 << ", id: " << std::to_string(connId);
Sunitha Harish29a82b02021-02-18 15:54:16 +0530124
125 auto respHandler =
126 [self(shared_from_this())](
127 const boost::beast::error_code ec,
128 const std::vector<boost::asio::ip::tcp::endpoint>&
129 endpointList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700130 if (ec || (endpointList.empty()))
131 {
132 BMCWEB_LOG_ERROR << "Resolve failed: " << ec.message();
133 self->state = ConnState::resolveFailed;
134 self->waitAndRetry();
135 return;
136 }
137 BMCWEB_LOG_DEBUG << "Resolved " << self->host << ":"
138 << std::to_string(self->port)
139 << ", id: " << std::to_string(self->connId);
140 self->doConnect(endpointList);
141 };
Carson Labradof52c03c2022-03-23 18:50:15 +0000142
Sunitha Harish29a82b02021-02-18 15:54:16 +0530143 resolver.asyncResolve(host, port, std::move(respHandler));
144 }
145
146 void doConnect(
147 const std::vector<boost::asio::ip::tcp::endpoint>& endpointList)
AppaRao Pulibd030d02020-03-20 03:34:29 +0530148 {
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530149 state = ConnState::connectInProgress;
150
Carson Labradof52c03c2022-03-23 18:50:15 +0000151 BMCWEB_LOG_DEBUG << "Trying to connect to: " << host << ":"
152 << std::to_string(port)
153 << ", id: " << std::to_string(connId);
Sunitha Harish29a82b02021-02-18 15:54:16 +0530154
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530155 conn.expires_after(std::chrono::seconds(30));
Ed Tanous002d39b2022-05-31 08:59:27 -0700156 conn.async_connect(endpointList,
157 [self(shared_from_this())](
158 const boost::beast::error_code ec,
159 const boost::asio::ip::tcp::endpoint& endpoint) {
160 if (ec)
161 {
162 BMCWEB_LOG_ERROR << "Connect " << endpoint.address().to_string()
163 << ":" << std::to_string(endpoint.port())
164 << ", id: " << std::to_string(self->connId)
165 << " failed: " << ec.message();
166 self->state = ConnState::connectFailed;
167 self->waitAndRetry();
168 return;
169 }
170 BMCWEB_LOG_DEBUG
171 << "Connected to: " << endpoint.address().to_string() << ":"
172 << std::to_string(endpoint.port())
173 << ", id: " << std::to_string(self->connId);
174 self->state = ConnState::connected;
175 self->sendMessage();
176 });
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530177 }
178
Carson Labradof52c03c2022-03-23 18:50:15 +0000179 void sendMessage()
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530180 {
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530181 state = ConnState::sendInProgress;
182
AppaRao Pulibd030d02020-03-20 03:34:29 +0530183 // Set a timeout on the operation
184 conn.expires_after(std::chrono::seconds(30));
185
186 // Send the HTTP request to the remote host
187 boost::beast::http::async_write(
188 conn, req,
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530189 [self(shared_from_this())](const boost::beast::error_code& ec,
AppaRao Pulibd030d02020-03-20 03:34:29 +0530190 const std::size_t& bytesTransferred) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700191 if (ec)
192 {
193 BMCWEB_LOG_ERROR << "sendMessage() failed: " << ec.message();
194 self->state = ConnState::sendFailed;
195 self->waitAndRetry();
196 return;
197 }
198 BMCWEB_LOG_DEBUG << "sendMessage() bytes transferred: "
199 << bytesTransferred;
200 boost::ignore_unused(bytesTransferred);
AppaRao Pulibd030d02020-03-20 03:34:29 +0530201
Ed Tanous002d39b2022-05-31 08:59:27 -0700202 self->recvMessage();
AppaRao Pulibd030d02020-03-20 03:34:29 +0530203 });
204 }
205
206 void recvMessage()
207 {
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530208 state = ConnState::recvInProgress;
209
210 parser.emplace(std::piecewise_construct, std::make_tuple());
211 parser->body_limit(httpReadBodyLimit);
212
AppaRao Pulibd030d02020-03-20 03:34:29 +0530213 // Receive the HTTP response
214 boost::beast::http::async_read(
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530215 conn, buffer, *parser,
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530216 [self(shared_from_this())](const boost::beast::error_code& ec,
AppaRao Pulibd030d02020-03-20 03:34:29 +0530217 const std::size_t& bytesTransferred) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700218 if (ec)
219 {
220 BMCWEB_LOG_ERROR << "recvMessage() failed: " << ec.message();
221 self->state = ConnState::recvFailed;
222 self->waitAndRetry();
223 return;
224 }
225 BMCWEB_LOG_DEBUG << "recvMessage() bytes transferred: "
226 << bytesTransferred;
227 BMCWEB_LOG_DEBUG << "recvMessage() data: "
228 << self->parser->get().body();
AppaRao Pulibd030d02020-03-20 03:34:29 +0530229
Ed Tanous002d39b2022-05-31 08:59:27 -0700230 unsigned int respCode = self->parser->get().result_int();
231 BMCWEB_LOG_DEBUG << "recvMessage() Header Response Code: "
232 << respCode;
233
234 // 2XX response is considered to be successful
235 if ((respCode < 200) || (respCode >= 300))
236 {
237 // The listener failed to receive the Sent-Event
238 BMCWEB_LOG_ERROR << "recvMessage() Listener Failed to "
239 "receive Sent-Event. Header Response Code: "
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530240 << respCode;
Ed Tanous002d39b2022-05-31 08:59:27 -0700241 self->state = ConnState::recvFailed;
242 self->waitAndRetry();
243 return;
244 }
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530245
Ed Tanous002d39b2022-05-31 08:59:27 -0700246 // Send is successful
247 // Reset the counter just in case this was after retrying
248 self->retryCount = 0;
AppaRao Pulibd030d02020-03-20 03:34:29 +0530249
Ed Tanous002d39b2022-05-31 08:59:27 -0700250 // Keep the connection alive if server supports it
251 // Else close the connection
252 BMCWEB_LOG_DEBUG << "recvMessage() keepalive : "
253 << self->parser->keep_alive();
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530254
Ed Tanous002d39b2022-05-31 08:59:27 -0700255 // Copy the response into a Response object so that it can be
256 // processed by the callback function.
257 self->res.clear();
258 self->res.stringResponse = self->parser->release();
259 self->callback(self->parser->keep_alive(), self->connId, self->res);
AppaRao Pulibd030d02020-03-20 03:34:29 +0530260 });
261 }
262
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530263 void waitAndRetry()
AppaRao Pulibd030d02020-03-20 03:34:29 +0530264 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000265 if (retryCount >= retryPolicy.maxRetryAttempts)
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530266 {
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530267 BMCWEB_LOG_ERROR << "Maximum number of retries reached.";
Carson Labradof52c03c2022-03-23 18:50:15 +0000268 BMCWEB_LOG_DEBUG << "Retry policy: "
269 << retryPolicy.retryPolicyAction;
Carson Labrado039a47e2022-04-05 16:03:20 +0000270
271 // We want to return a 502 to indicate there was an error with the
272 // external server
273 res.clear();
274 redfish::messages::operationFailed(res);
275
Carson Labradof52c03c2022-03-23 18:50:15 +0000276 if (retryPolicy.retryPolicyAction == "TerminateAfterRetries")
Ayushi Smritife44eb02020-05-15 15:24:45 +0530277 {
278 // TODO: delete subscription
279 state = ConnState::terminated;
Carson Labrado039a47e2022-04-05 16:03:20 +0000280 callback(false, connId, res);
Ayushi Smritife44eb02020-05-15 15:24:45 +0530281 }
Carson Labradof52c03c2022-03-23 18:50:15 +0000282 if (retryPolicy.retryPolicyAction == "SuspendRetries")
Ayushi Smritife44eb02020-05-15 15:24:45 +0530283 {
284 state = ConnState::suspended;
Carson Labrado039a47e2022-04-05 16:03:20 +0000285 callback(false, connId, res);
Ayushi Smritife44eb02020-05-15 15:24:45 +0530286 }
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530287 // Reset the retrycount to zero so that client can try connecting
288 // again if needed
Ed Tanous3174e4d2020-10-07 11:41:22 -0700289 retryCount = 0;
Ayushi Smritife44eb02020-05-15 15:24:45 +0530290 return;
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530291 }
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530292
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530293 if (runningTimer)
294 {
295 BMCWEB_LOG_DEBUG << "Retry timer is already running.";
296 return;
297 }
298 runningTimer = true;
299
300 retryCount++;
301
Carson Labradof52c03c2022-03-23 18:50:15 +0000302 BMCWEB_LOG_DEBUG << "Attempt retry after "
303 << std::to_string(
304 retryPolicy.retryIntervalSecs.count())
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530305 << " seconds. RetryCount = " << retryCount;
Carson Labradof52c03c2022-03-23 18:50:15 +0000306 timer.expires_after(retryPolicy.retryIntervalSecs);
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530307 timer.async_wait(
Carson Labradof52c03c2022-03-23 18:50:15 +0000308 [self(shared_from_this())](const boost::system::error_code ec) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700309 if (ec == boost::asio::error::operation_aborted)
310 {
311 BMCWEB_LOG_DEBUG
312 << "async_wait failed since the operation is aborted"
313 << ec.message();
314 }
315 else if (ec)
316 {
317 BMCWEB_LOG_ERROR << "async_wait failed: " << ec.message();
318 // Ignore the error and continue the retry loop to attempt
319 // sending the event as per the retry policy
320 }
321 self->runningTimer = false;
Sunitha Harish6eaa1d22021-02-19 13:38:31 +0530322
Ed Tanous002d39b2022-05-31 08:59:27 -0700323 // Let's close the connection and restart from resolve.
324 self->doCloseAndRetry();
325 });
Ayushi Smritife44eb02020-05-15 15:24:45 +0530326 }
327
Carson Labradof52c03c2022-03-23 18:50:15 +0000328 void doClose()
Ayushi Smritife44eb02020-05-15 15:24:45 +0530329 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000330 state = ConnState::closeInProgress;
331 boost::beast::error_code ec;
332 conn.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
333 conn.close();
334
335 // not_connected happens sometimes so don't bother reporting it.
336 if (ec && ec != boost::beast::errc::not_connected)
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530337 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000338 BMCWEB_LOG_ERROR << host << ":" << std::to_string(port)
339 << ", id: " << std::to_string(connId)
340 << "shutdown failed: " << ec.message();
341 return;
342 }
343 BMCWEB_LOG_DEBUG << host << ":" << std::to_string(port)
344 << ", id: " << std::to_string(connId)
345 << " closed gracefully";
346 if ((state != ConnState::suspended) && (state != ConnState::terminated))
347 {
348 state = ConnState::closed;
349 }
350 }
351
352 void doCloseAndRetry()
353 {
354 state = ConnState::closeInProgress;
355 boost::beast::error_code ec;
356 conn.socket().shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
357 conn.close();
358
359 // not_connected happens sometimes so don't bother reporting it.
360 if (ec && ec != boost::beast::errc::not_connected)
361 {
362 BMCWEB_LOG_ERROR << host << ":" << std::to_string(port)
363 << ", id: " << std::to_string(connId)
364 << "shutdown failed: " << ec.message();
365 return;
366 }
367 BMCWEB_LOG_DEBUG << host << ":" << std::to_string(port)
368 << ", id: " << std::to_string(connId)
369 << " closed gracefully";
370 if ((state != ConnState::suspended) && (state != ConnState::terminated))
371 {
372 // Now let's try to resend the data
373 state = ConnState::retry;
374 this->doResolve();
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530375 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530376 }
377
378 public:
Carson Labradof52c03c2022-03-23 18:50:15 +0000379 explicit ConnectionInfo(boost::asio::io_context& ioc, const std::string& id,
380 const std::string& destIP, const uint16_t destPort,
Carson Labradof52c03c2022-03-23 18:50:15 +0000381 const unsigned int connId) :
382 subId(id),
Carson Labrado244256c2022-04-27 17:16:32 +0000383 host(destIP), port(destPort), connId(connId), conn(ioc), timer(ioc)
384 {}
Carson Labradof52c03c2022-03-23 18:50:15 +0000385};
AppaRao Pulibd030d02020-03-20 03:34:29 +0530386
Carson Labradof52c03c2022-03-23 18:50:15 +0000387class ConnectionPool : public std::enable_shared_from_this<ConnectionPool>
388{
389 private:
390 boost::asio::io_context& ioc;
391 const std::string id;
392 const std::string destIP;
393 const uint16_t destPort;
Carson Labradof52c03c2022-03-23 18:50:15 +0000394 std::vector<std::shared_ptr<ConnectionInfo>> connections;
395 boost::container::devector<PendingRequest> requestQueue;
396
397 friend class HttpClient;
398
Carson Labrado244256c2022-04-27 17:16:32 +0000399 // Configure a connections's request, callback, and retry info in
400 // preparation to begin sending the request
Carson Labradof52c03c2022-03-23 18:50:15 +0000401 void setConnProps(ConnectionInfo& conn)
AppaRao Pulibd030d02020-03-20 03:34:29 +0530402 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000403 if (requestQueue.empty())
AppaRao Pulibd030d02020-03-20 03:34:29 +0530404 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000405 BMCWEB_LOG_ERROR
406 << "setConnProps() should not have been called when requestQueue is empty";
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530407 return;
AppaRao Pulibd030d02020-03-20 03:34:29 +0530408 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530409
Carson Labrado244256c2022-04-27 17:16:32 +0000410 auto nextReq = requestQueue.front();
411 conn.retryPolicy = std::move(nextReq.retryPolicy);
412 conn.req = std::move(nextReq.req);
413 conn.callback = std::move(nextReq.callback);
Carson Labradof52c03c2022-03-23 18:50:15 +0000414
415 BMCWEB_LOG_DEBUG << "Setting properties for connection " << conn.host
416 << ":" << std::to_string(conn.port)
417 << ", id: " << std::to_string(conn.connId)
418 << ", retry policy is \"" << conn.retryPolicy.name
419 << "\"";
420
421 // We can remove the request from the queue at this point
422 requestQueue.pop_front();
423 }
424
425 // Configures a connection to use the specific retry policy.
426 inline void setConnRetryPolicy(ConnectionInfo& conn,
427 const RetryPolicyData& retryPolicy)
428 {
429 BMCWEB_LOG_DEBUG << destIP << ":" << std::to_string(destPort)
430 << ", id: " << std::to_string(conn.connId)
431 << " using retry policy \"" << retryPolicy.name
432 << "\"";
433
434 conn.retryPolicy = retryPolicy;
435 }
436
437 // Gets called as part of callback after request is sent
438 // Reuses the connection if there are any requests waiting to be sent
439 // Otherwise closes the connection if it is not a keep-alive
440 void sendNext(bool keepAlive, uint32_t connId)
441 {
442 auto conn = connections[connId];
443 // Reuse the connection to send the next request in the queue
444 if (!requestQueue.empty())
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530445 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000446 BMCWEB_LOG_DEBUG << std::to_string(requestQueue.size())
447 << " requests remaining in queue for " << destIP
448 << ":" << std::to_string(destPort)
449 << ", reusing connnection "
450 << std::to_string(connId);
451
452 setConnProps(*conn);
453
454 if (keepAlive)
455 {
456 conn->sendMessage();
457 }
458 else
459 {
460 // Server is not keep-alive enabled so we need to close the
461 // connection and then start over from resolve
462 conn->doClose();
463 conn->doResolve();
464 }
465 return;
466 }
467
468 // No more messages to send so close the connection if necessary
469 if (keepAlive)
470 {
471 conn->state = ConnState::idle;
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530472 }
473 else
474 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000475 // Abort the connection since server is not keep-alive enabled
476 conn->state = ConnState::abortConnection;
477 conn->doClose();
AppaRao Puli2a5689a2020-04-29 15:24:31 +0530478 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530479 }
480
Carson Labrado244256c2022-04-27 17:16:32 +0000481 void sendData(std::string& data, const std::string& destUri,
482 const boost::beast::http::fields& httpHeader,
483 const boost::beast::http::verb verb,
484 const RetryPolicyData& retryPolicy,
Carson Labrado039a47e2022-04-05 16:03:20 +0000485 std::function<void(Response&)>& resHandler)
Ayushi Smritife44eb02020-05-15 15:24:45 +0530486 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000487 std::weak_ptr<ConnectionPool> weakSelf = weak_from_this();
488
489 // Callback to be called once the request has been sent
Carson Labrado039a47e2022-04-05 16:03:20 +0000490 auto cb = [weakSelf, resHandler](bool keepAlive, uint32_t connId,
491 Response& res) {
492 // Allow provided callback to perform additional processing of the
493 // request
494 resHandler(res);
495
Carson Labradof52c03c2022-03-23 18:50:15 +0000496 // If requests remain in the queue then we want to reuse this
497 // connection to send the next request
498 std::shared_ptr<ConnectionPool> self = weakSelf.lock();
499 if (!self)
500 {
501 BMCWEB_LOG_CRITICAL << self << " Failed to capture connection";
502 return;
503 }
504
505 self->sendNext(keepAlive, connId);
506 };
507
Carson Labrado244256c2022-04-27 17:16:32 +0000508 // Construct the request to be sent
509 boost::beast::http::request<boost::beast::http::string_body> thisReq(
510 verb, destUri, 11, "", httpHeader);
511 thisReq.set(boost::beast::http::field::host, destIP);
512 thisReq.keep_alive(true);
513 thisReq.body() = std::move(data);
514 thisReq.prepare_payload();
515
Carson Labradof52c03c2022-03-23 18:50:15 +0000516 // Reuse an existing connection if one is available
517 for (unsigned int i = 0; i < connections.size(); i++)
518 {
519 auto conn = connections[i];
520 if ((conn->state == ConnState::idle) ||
521 (conn->state == ConnState::initialized) ||
522 (conn->state == ConnState::closed))
523 {
Carson Labrado244256c2022-04-27 17:16:32 +0000524 conn->req = std::move(thisReq);
Carson Labradof52c03c2022-03-23 18:50:15 +0000525 conn->callback = std::move(cb);
Carson Labradof52c03c2022-03-23 18:50:15 +0000526 setConnRetryPolicy(*conn, retryPolicy);
527 std::string commonMsg = std::to_string(i) + " from pool " +
528 destIP + ":" + std::to_string(destPort);
529
530 if (conn->state == ConnState::idle)
531 {
532 BMCWEB_LOG_DEBUG << "Grabbing idle connection "
533 << commonMsg;
534 conn->sendMessage();
535 }
536 else
537 {
538 BMCWEB_LOG_DEBUG << "Reusing existing connection "
539 << commonMsg;
540 conn->doResolve();
541 }
542 return;
543 }
544 }
545
546 // All connections in use so create a new connection or add request to
547 // the queue
548 if (connections.size() < maxPoolSize)
549 {
550 BMCWEB_LOG_DEBUG << "Adding new connection to pool " << destIP
551 << ":" << std::to_string(destPort);
552 auto conn = addConnection();
Carson Labrado244256c2022-04-27 17:16:32 +0000553 conn->req = std::move(thisReq);
Carson Labradof52c03c2022-03-23 18:50:15 +0000554 conn->callback = std::move(cb);
555 setConnRetryPolicy(*conn, retryPolicy);
556 conn->doResolve();
557 }
558 else if (requestQueue.size() < maxRequestQueueSize)
559 {
560 BMCWEB_LOG_ERROR << "Max pool size reached. Adding data to queue.";
Carson Labrado244256c2022-04-27 17:16:32 +0000561 requestQueue.emplace_back(std::move(thisReq), std::move(cb),
Carson Labradof52c03c2022-03-23 18:50:15 +0000562 retryPolicy);
563 }
564 else
565 {
566 BMCWEB_LOG_ERROR << destIP << ":" << std::to_string(destPort)
567 << " request queue full. Dropping request.";
568 }
Ayushi Smritife44eb02020-05-15 15:24:45 +0530569 }
570
Carson Labradof52c03c2022-03-23 18:50:15 +0000571 std::shared_ptr<ConnectionInfo>& addConnection()
Ayushi Smritife44eb02020-05-15 15:24:45 +0530572 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000573 unsigned int newId = static_cast<unsigned int>(connections.size());
574
Carson Labrado244256c2022-04-27 17:16:32 +0000575 auto& ret = connections.emplace_back(
576 std::make_shared<ConnectionInfo>(ioc, id, destIP, destPort, newId));
Carson Labradof52c03c2022-03-23 18:50:15 +0000577
578 BMCWEB_LOG_DEBUG << "Added connection "
579 << std::to_string(connections.size() - 1)
580 << " to pool " << destIP << ":"
581 << std::to_string(destPort);
582
583 return ret;
584 }
585
586 public:
587 explicit ConnectionPool(boost::asio::io_context& ioc, const std::string& id,
Carson Labrado244256c2022-04-27 17:16:32 +0000588 const std::string& destIP,
589 const uint16_t destPort) :
Carson Labradof52c03c2022-03-23 18:50:15 +0000590 ioc(ioc),
Carson Labrado244256c2022-04-27 17:16:32 +0000591 id(id), destIP(destIP), destPort(destPort)
Carson Labradof52c03c2022-03-23 18:50:15 +0000592 {
593 std::string clientKey = destIP + ":" + std::to_string(destPort);
594 BMCWEB_LOG_DEBUG << "Initializing connection pool for " << destIP << ":"
595 << std::to_string(destPort);
596
597 // Initialize the pool with a single connection
598 addConnection();
Ayushi Smritife44eb02020-05-15 15:24:45 +0530599 }
AppaRao Pulibd030d02020-03-20 03:34:29 +0530600};
601
Carson Labradof52c03c2022-03-23 18:50:15 +0000602class HttpClient
603{
604 private:
605 std::unordered_map<std::string, std::shared_ptr<ConnectionPool>>
606 connectionPools;
607 boost::asio::io_context& ioc =
608 crow::connections::systemBus->get_io_context();
609 std::unordered_map<std::string, RetryPolicyData> retryInfo;
610 HttpClient() = default;
611
Carson Labrado039a47e2022-04-05 16:03:20 +0000612 // Used as a dummy callback by sendData() in order to call
613 // sendDataWithCallback()
614 static void genericResHandler(Response& res)
615 {
616 BMCWEB_LOG_DEBUG << "Response handled with return code: "
617 << std::to_string(res.resultInt());
618 };
619
Carson Labradof52c03c2022-03-23 18:50:15 +0000620 public:
621 HttpClient(const HttpClient&) = delete;
622 HttpClient& operator=(const HttpClient&) = delete;
623 HttpClient(HttpClient&&) = delete;
624 HttpClient& operator=(HttpClient&&) = delete;
625 ~HttpClient() = default;
626
627 static HttpClient& getInstance()
628 {
629 static HttpClient handler;
630 return handler;
631 }
632
Carson Labrado039a47e2022-04-05 16:03:20 +0000633 // Send a request to destIP:destPort where additional processing of the
634 // result is not required
Carson Labradof52c03c2022-03-23 18:50:15 +0000635 void sendData(std::string& data, const std::string& id,
636 const std::string& destIP, const uint16_t destPort,
637 const std::string& destUri,
638 const boost::beast::http::fields& httpHeader,
Carson Labrado244256c2022-04-27 17:16:32 +0000639 const boost::beast::http::verb verb,
640 const std::string& retryPolicyName)
Carson Labradof52c03c2022-03-23 18:50:15 +0000641 {
Carson Labrado039a47e2022-04-05 16:03:20 +0000642 std::function<void(Response&)> cb = genericResHandler;
643 sendDataWithCallback(data, id, destIP, destPort, destUri, httpHeader,
Carson Labrado244256c2022-04-27 17:16:32 +0000644 verb, retryPolicyName, cb);
Carson Labrado039a47e2022-04-05 16:03:20 +0000645 }
646
647 // Send request to destIP:destPort and use the provided callback to
648 // handle the response
649 void sendDataWithCallback(std::string& data, const std::string& id,
650 const std::string& destIP,
651 const uint16_t destPort,
652 const std::string& destUri,
653 const boost::beast::http::fields& httpHeader,
Carson Labrado244256c2022-04-27 17:16:32 +0000654 const boost::beast::http::verb verb,
655 const std::string& retryPolicyName,
Carson Labrado039a47e2022-04-05 16:03:20 +0000656 std::function<void(Response&)>& resHandler)
657 {
Carson Labradof52c03c2022-03-23 18:50:15 +0000658 std::string clientKey = destIP + ":" + std::to_string(destPort);
659 // Use nullptr to avoid creating a ConnectionPool each time
660 auto result = connectionPools.try_emplace(clientKey, nullptr);
661 if (result.second)
662 {
663 // Now actually create the ConnectionPool shared_ptr since it does
664 // not already exist
Carson Labrado244256c2022-04-27 17:16:32 +0000665 result.first->second =
666 std::make_shared<ConnectionPool>(ioc, id, destIP, destPort);
Carson Labradof52c03c2022-03-23 18:50:15 +0000667 BMCWEB_LOG_DEBUG << "Created connection pool for " << clientKey;
668 }
669 else
670 {
671 BMCWEB_LOG_DEBUG << "Using existing connection pool for "
672 << clientKey;
673 }
674
675 // Get the associated retry policy
676 auto policy = retryInfo.try_emplace(retryPolicyName);
677 if (policy.second)
678 {
679 BMCWEB_LOG_DEBUG << "Creating retry policy \"" << retryPolicyName
680 << "\" with default values";
681 policy.first->second.name = retryPolicyName;
682 }
683
684 // Send the data using either the existing connection pool or the newly
685 // created connection pool
Carson Labrado244256c2022-04-27 17:16:32 +0000686 result.first->second->sendData(data, destUri, httpHeader, verb,
687 policy.first->second, resHandler);
Carson Labradof52c03c2022-03-23 18:50:15 +0000688 }
689
690 void setRetryConfig(const uint32_t retryAttempts,
691 const uint32_t retryTimeoutInterval,
692 const std::string& retryPolicyName)
693 {
694 // We need to create the retry policy if one does not already exist for
695 // the given retryPolicyName
696 auto result = retryInfo.try_emplace(retryPolicyName);
697 if (result.second)
698 {
699 BMCWEB_LOG_DEBUG << "setRetryConfig(): Creating new retry policy \""
700 << retryPolicyName << "\"";
701 result.first->second.name = retryPolicyName;
702 }
703 else
704 {
705 BMCWEB_LOG_DEBUG << "setRetryConfig(): Updating retry info for \""
706 << retryPolicyName << "\"";
707 }
708
709 result.first->second.maxRetryAttempts = retryAttempts;
710 result.first->second.retryIntervalSecs =
711 std::chrono::seconds(retryTimeoutInterval);
712 }
713
714 void setRetryPolicy(const std::string& retryPolicy,
715 const std::string& retryPolicyName)
716 {
717 // We need to create the retry policy if one does not already exist for
718 // the given retryPolicyName
719 auto result = retryInfo.try_emplace(retryPolicyName);
720 if (result.second)
721 {
722 BMCWEB_LOG_DEBUG << "setRetryPolicy(): Creating new retry policy \""
723 << retryPolicyName << "\"";
724 result.first->second.name = retryPolicyName;
725 }
726 else
727 {
728 BMCWEB_LOG_DEBUG << "setRetryPolicy(): Updating retry policy for \""
729 << retryPolicyName << "\"";
730 }
731
732 result.first->second.retryPolicyAction = retryPolicy;
733 }
734};
AppaRao Pulibd030d02020-03-20 03:34:29 +0530735} // namespace crow