|  | 
| 1 | 1 | #include <drogon/drogon.h> | 
| 2 | 2 | 
 | 
| 3 |  | -#include <future> | 
|  | 3 | +#include <chrono> | 
| 4 | 4 | #include <iostream> | 
|  | 5 | +#include <memory> | 
|  | 6 | +#include <thread> | 
|  | 7 | +#include <drogon/HttpTypes.h> | 
|  | 8 | +#include <trantor/utils/Logger.h> | 
| 5 | 9 | 
 | 
| 6 | 10 | #ifdef __linux__ | 
| 7 | 11 | #include <sys/socket.h> | 
| 8 | 12 | #include <netinet/tcp.h> | 
| 9 | 13 | #endif | 
| 10 |  | - | 
|  | 14 | +#include <drogon/utils/HttpClientPool.h> | 
| 11 | 15 | using namespace drogon; | 
| 12 | 16 | 
 | 
| 13 | 17 | int nth_resp = 0; | 
| 14 | 18 | 
 | 
| 15 | 19 | int main() | 
| 16 | 20 | { | 
|  | 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 | +    }; | 
| 17 | 42 |     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 | 
| 18 | 107 |     { | 
| 19 | 108 |         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); | 
| 41 | 110 | 
 | 
| 42 | 111 |         auto req = HttpRequest::newHttpRequest(); | 
| 43 | 112 |         req->setMethod(drogon::Get); | 
| @@ -77,4 +146,5 @@ int main() | 
| 77 | 146 |     } | 
| 78 | 147 | 
 | 
| 79 | 148 |     app().run(); | 
|  | 149 | +#endif | 
| 80 | 150 | } | 
0 commit comments