diff --git a/examples/client_example/main.cc b/examples/client_example/main.cc index d991c5a3..d783f54e 100644 --- a/examples/client_example/main.cc +++ b/examples/client_example/main.cc @@ -72,6 +72,8 @@ int main() std::cout << "count=" << nth_resp << std::endl; }); } + std::cout << "requestsBufferSize:" << client->requestsBufferSize() + << std::endl; } app().run(); diff --git a/lib/inc/drogon/HttpClient.h b/lib/inc/drogon/HttpClient.h index 73b60b7e..35170d20 100644 --- a/lib/inc/drogon/HttpClient.h +++ b/lib/inc/drogon/HttpClient.h @@ -21,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -178,6 +179,12 @@ class DROGON_EXPORT HttpClient : public trantor::NonCopyable */ virtual void setSockOptCallback(std::function cb) = 0; + /** + * @brief Return the number of unsent http requests in the current http + * client cache buffer + */ + virtual std::size_t requestsBufferSize() = 0; + /// Set the pipelining depth, which is the number of requests that are not /// responding. /** diff --git a/lib/src/HttpClientImpl.h b/lib/src/HttpClientImpl.h index bf697556..c0ef9fca 100644 --- a/lib/src/HttpClientImpl.h +++ b/lib/src/HttpClientImpl.h @@ -19,6 +19,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -115,6 +118,21 @@ class HttpClientImpl final : public HttpClient, sockOptCallback_ = std::move(cb); } + std::size_t requestsBufferSize() override + { + if (loop_->isInLoopThread()) + { + return requestsBuffer_.size(); + } + else + { + std::promise bufferSize; + loop_->queueInLoop( + [&] { bufferSize.set_value(requestsBuffer_.size()); }); + return bufferSize.get_future().get(); + } + } + private: std::shared_ptr tcpClientPtr_; trantor::EventLoop *loop_;