Add requestsBufferSize function (#2124)

This commit is contained in:
fantasy-peak 2024-08-14 11:15:33 +08:00 committed by GitHub
parent 206ef0d881
commit 5b5d1906bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 0 deletions

View File

@ -72,6 +72,8 @@ int main()
std::cout << "count=" << nth_resp << std::endl; std::cout << "count=" << nth_resp << std::endl;
}); });
} }
std::cout << "requestsBufferSize:" << client->requestsBufferSize()
<< std::endl;
} }
app().run(); app().run();

View File

@ -21,6 +21,7 @@
#include <drogon/HttpRequest.h> #include <drogon/HttpRequest.h>
#include <trantor/utils/NonCopyable.h> #include <trantor/utils/NonCopyable.h>
#include <trantor/net/EventLoop.h> #include <trantor/net/EventLoop.h>
#include <cstddef>
#include <functional> #include <functional>
#include <memory> #include <memory>
#include <future> #include <future>
@ -178,6 +179,12 @@ class DROGON_EXPORT HttpClient : public trantor::NonCopyable
*/ */
virtual void setSockOptCallback(std::function<void(int)> cb) = 0; virtual void setSockOptCallback(std::function<void(int)> 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 /// Set the pipelining depth, which is the number of requests that are not
/// responding. /// responding.
/** /**

View File

@ -19,6 +19,9 @@
#include <trantor/net/EventLoop.h> #include <trantor/net/EventLoop.h>
#include <trantor/net/Resolver.h> #include <trantor/net/Resolver.h>
#include <trantor/net/TcpClient.h> #include <trantor/net/TcpClient.h>
#include <cstddef>
#include <functional>
#include <future>
#include <list> #include <list>
#include <mutex> #include <mutex>
#include <queue> #include <queue>
@ -115,6 +118,21 @@ class HttpClientImpl final : public HttpClient,
sockOptCallback_ = std::move(cb); sockOptCallback_ = std::move(cb);
} }
std::size_t requestsBufferSize() override
{
if (loop_->isInLoopThread())
{
return requestsBuffer_.size();
}
else
{
std::promise<std::size_t> bufferSize;
loop_->queueInLoop(
[&] { bufferSize.set_value(requestsBuffer_.size()); });
return bufferSize.get_future().get();
}
}
private: private:
std::shared_ptr<trantor::TcpClient> tcpClientPtr_; std::shared_ptr<trantor::TcpClient> tcpClientPtr_;
trantor::EventLoop *loop_; trantor::EventLoop *loop_;