Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HttpClient pool #2187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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/Http11ClientPool.h
lib/inc/drogon/utils/FunctionTraits.h
lib/inc/drogon/utils/HttpConstraint.h
lib/inc/drogon/utils/OStringStream.h
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/Http11ClientPool.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
Http11ClientPoolConfig 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<Http11ClientPool>(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);
@@ -77,4 +146,5 @@ int main()
}

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