Skip to content

Commit 80c3720

Browse files
committed
Add HttpClient pool
1 parent ca22103 commit 80c3720

File tree

3 files changed

+408
-23
lines changed

3 files changed

+408
-23
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,7 @@ install(FILES ${NOSQL_HEADERS} DESTINATION ${INSTALL_INCLUDE_DIR}/drogon/nosql)
717717

718718
set(DROGON_UTIL_HEADERS
719719
lib/inc/drogon/utils/coroutine.h
720+
lib/inc/drogon/utils/HttpClientPool.h
720721
lib/inc/drogon/utils/FunctionTraits.h
721722
lib/inc/drogon/utils/HttpConstraint.h
722723
lib/inc/drogon/utils/OStringStream.h

examples/client_example/main.cc

Lines changed: 93 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,112 @@
11
#include <drogon/drogon.h>
22

3-
#include <future>
3+
#include <chrono>
44
#include <iostream>
5+
#include <memory>
6+
#include <thread>
7+
#include <drogon/HttpTypes.h>
8+
#include <trantor/utils/Logger.h>
59

610
#ifdef __linux__
711
#include <sys/socket.h>
812
#include <netinet/tcp.h>
913
#endif
10-
14+
#include <drogon/utils/HttpClientPool.h>
1115
using namespace drogon;
1216

1317
int nth_resp = 0;
1418

1519
int main()
1620
{
21+
auto func = [](int fd) {
22+
std::cout << "setSockOptCallback:" << fd << std::endl;
23+
#ifdef __linux__
24+
int optval = 10;
25+
::setsockopt(fd,
26+
SOL_TCP,
27+
TCP_KEEPCNT,
28+
&optval,
29+
static_cast<socklen_t>(sizeof optval));
30+
::setsockopt(fd,
31+
SOL_TCP,
32+
TCP_KEEPIDLE,
33+
&optval,
34+
static_cast<socklen_t>(sizeof optval));
35+
::setsockopt(fd,
36+
SOL_TCP,
37+
TCP_KEEPINTVL,
38+
&optval,
39+
static_cast<socklen_t>(sizeof optval));
40+
#endif
41+
};
1742
trantor::Logger::setLogLevel(trantor::Logger::kTrace);
43+
#ifdef __cpp_impl_coroutine
44+
HttpClientPoolConfig cfg{
45+
.hostString = "http://www.baidu.com",
46+
.useOldTLS = false,
47+
.validateCert = false,
48+
.size = 10,
49+
.setCallback =
50+
[func](auto &client) {
51+
LOG_INFO << "setCallback";
52+
client->setSockOptCallback(func);
53+
},
54+
.numOfThreads = 4,
55+
.keepaliveRequests = 1000,
56+
.idleTimeout = std::chrono::seconds(10),
57+
.maxLifeTime = std::chrono::seconds(300),
58+
.checkInterval = std::chrono::seconds(10),
59+
};
60+
auto pool = std::make_unique<HttpClientPool>(cfg);
61+
auto req = HttpRequest::newHttpRequest();
62+
req->setMethod(drogon::Get);
63+
req->setPath("/s");
64+
req->setParameter("wd", "wx");
65+
req->setParameter("oq", "wx");
66+
67+
for (int i = 0; i < 1; i++)
68+
{
69+
[](auto req, auto &pool) -> drogon::AsyncTask {
70+
{
71+
auto [result, resp] = co_await pool->sendRequestCoro(req, 10);
72+
if (result == ReqResult::Ok)
73+
LOG_INFO << "1:" << resp->getStatusCode();
74+
}
75+
{
76+
auto [result, resp] = co_await pool->sendRequestCoro(req, 10);
77+
if (result == ReqResult::Ok)
78+
LOG_INFO << "2:" << resp->getStatusCode();
79+
}
80+
{
81+
auto [result, resp] = co_await pool->sendRequestCoro(req, 10);
82+
if (result == ReqResult::Ok)
83+
LOG_INFO << "3:" << resp->getStatusCode();
84+
}
85+
co_return;
86+
}(req, pool);
87+
}
88+
89+
for (int i = 0; i < 10; i++)
90+
{
91+
pool->sendRequest(
92+
req,
93+
[](ReqResult result, const HttpResponsePtr &response) {
94+
if (result != ReqResult::Ok)
95+
{
96+
LOG_ERROR
97+
<< "error while sending request to server! result: "
98+
<< result;
99+
return;
100+
}
101+
LOG_INFO << "callback:" << response->getStatusCode();
102+
},
103+
10);
104+
}
105+
std::this_thread::sleep_for(std::chrono::seconds(30));
106+
#else
18107
{
19108
auto client = HttpClient::newHttpClient("http://www.baidu.com");
20-
client->setSockOptCallback([](int fd) {
21-
std::cout << "setSockOptCallback:" << fd << std::endl;
22-
#ifdef __linux__
23-
int optval = 10;
24-
::setsockopt(fd,
25-
SOL_TCP,
26-
TCP_KEEPCNT,
27-
&optval,
28-
static_cast<socklen_t>(sizeof optval));
29-
::setsockopt(fd,
30-
SOL_TCP,
31-
TCP_KEEPIDLE,
32-
&optval,
33-
static_cast<socklen_t>(sizeof optval));
34-
::setsockopt(fd,
35-
SOL_TCP,
36-
TCP_KEEPINTVL,
37-
&optval,
38-
static_cast<socklen_t>(sizeof optval));
39-
#endif
40-
});
109+
client->setSockOptCallback(func);
41110

42111
auto req = HttpRequest::newHttpRequest();
43112
req->setMethod(drogon::Get);
@@ -77,4 +146,5 @@ int main()
77146
}
78147

79148
app().run();
149+
#endif
80150
}

0 commit comments

Comments
 (0)