Skip to content

Commit

Permalink
Add HttpClient pool
Browse files Browse the repository at this point in the history
  • Loading branch information
fantasy-peak committed Oct 18, 2024
1 parent ca22103 commit c9982db
Show file tree
Hide file tree
Showing 3 changed files with 415 additions and 23 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ install(FILES ${NOSQL_HEADERS} DESTINATION ${INSTALL_INCLUDE_DIR}/drogon/nosql)

set(DROGON_UTIL_HEADERS
lib/inc/drogon/utils/coroutine.h
lib/inc/drogon/utils/HttpClientPool.h
lib/inc/drogon/utils/FunctionTraits.h
lib/inc/drogon/utils/HttpConstraint.h
lib/inc/drogon/utils/OStringStream.h
Expand Down
116 changes: 93 additions & 23 deletions examples/client_example/main.cc
Original file line number Diff line number Diff line change
@@ -1,43 +1,112 @@
#include <drogon/drogon.h>

#include <future>
#include <chrono>
#include <iostream>
#include <memory>
#include <thread>
#include <drogon/HttpTypes.h>
#include <trantor/utils/Logger.h>

#ifdef __linux__
#include <sys/socket.h>
#include <netinet/tcp.h>
#endif

#include <drogon/utils/HttpClientPool.h>
using namespace drogon;

int nth_resp = 0;

int main()
{
auto func = [](int fd) {
std::cout << "setSockOptCallback:" << fd << std::endl;
#ifdef __linux__
int optval = 10;
::setsockopt(fd,
SOL_TCP,
TCP_KEEPCNT,
&optval,
static_cast<socklen_t>(sizeof optval));
::setsockopt(fd,
SOL_TCP,
TCP_KEEPIDLE,
&optval,
static_cast<socklen_t>(sizeof optval));
::setsockopt(fd,
SOL_TCP,
TCP_KEEPINTVL,
&optval,
static_cast<socklen_t>(sizeof optval));
#endif
};
trantor::Logger::setLogLevel(trantor::Logger::kTrace);
#ifdef __cpp_impl_coroutine
HttpClientPoolConfig cfg{
.hostString = "http://www.baidu.com",
.useOldTLS = false,
.validateCert = false,
.size = 10,
.setCallback =
[func](auto &client) {
LOG_INFO << "setCallback";
client->setSockOptCallback(func);
},
.numOfThreads = 4,
.keepaliveRequests = 1000,
.idleTimeout = std::chrono::seconds(10),
.maxLifeTime = std::chrono::seconds(300),
.checkInterval = std::chrono::seconds(10),
};
auto pool = std::make_unique<HttpClientPool>(cfg);
auto req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
req->setPath("/s");
req->setParameter("wd", "wx");
req->setParameter("oq", "wx");

for (int i = 0; i < 1; i++)
{
[](auto req, auto &pool) -> drogon::AsyncTask {
{
auto [result, resp] = co_await pool->sendRequestCoro(req, 10);
if (result == ReqResult::Ok)
LOG_INFO << "1:" << resp->getStatusCode();
}
{
auto [result, resp] = co_await pool->sendRequestCoro(req, 10);
if (result == ReqResult::Ok)
LOG_INFO << "2:" << resp->getStatusCode();
}
{
auto [result, resp] = co_await pool->sendRequestCoro(req, 10);
if (result == ReqResult::Ok)
LOG_INFO << "3:" << resp->getStatusCode();
}
co_return;
}(req, pool);
}

for (int i = 0; i < 10; i++)
{
pool->sendRequest(
req,
[](ReqResult result, const HttpResponsePtr &response) {
if (result != ReqResult::Ok)
{
LOG_ERROR
<< "error while sending request to server! result: "
<< result;
return;
}
LOG_INFO << "callback:" << response->getStatusCode();
},
10);
}
std::this_thread::sleep_for(std::chrono::seconds(30));
#else
{
auto client = HttpClient::newHttpClient("http://www.baidu.com");
client->setSockOptCallback([](int fd) {
std::cout << "setSockOptCallback:" << fd << std::endl;
#ifdef __linux__
int optval = 10;
::setsockopt(fd,
SOL_TCP,
TCP_KEEPCNT,
&optval,
static_cast<socklen_t>(sizeof optval));
::setsockopt(fd,
SOL_TCP,
TCP_KEEPIDLE,
&optval,
static_cast<socklen_t>(sizeof optval));
::setsockopt(fd,
SOL_TCP,
TCP_KEEPINTVL,
&optval,
static_cast<socklen_t>(sizeof optval));
#endif
});
client->setSockOptCallback(func);

auto req = HttpRequest::newHttpRequest();
req->setMethod(drogon::Get);
Expand Down Expand Up @@ -77,4 +146,5 @@ int main()
}

app().run();
#endif
}
Loading

0 comments on commit c9982db

Please sign in to comment.