Skip to content

Commit b5b910e

Browse files
committed
Add HttpClient pool
1 parent ca22103 commit b5b910e

File tree

3 files changed

+416
-23
lines changed

3 files changed

+416
-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: 94 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,113 @@
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 <drogon/utils/coroutine.h>
9+
#include <trantor/utils/Logger.h>
510

611
#ifdef __linux__
712
#include <sys/socket.h>
813
#include <netinet/tcp.h>
914
#endif
10-
15+
#include <drogon/utils/HttpClientPool.h>
1116
using namespace drogon;
1217

1318
int nth_resp = 0;
1419

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

42112
auto req = HttpRequest::newHttpRequest();
43113
req->setMethod(drogon::Get);
@@ -77,4 +147,5 @@ int main()
77147
}
78148

79149
app().run();
150+
#endif
80151
}

0 commit comments

Comments
 (0)