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 << "requestsBufferSize:" << client->requestsBufferSize()
<< std::endl;
}
app().run();

View File

@ -21,6 +21,7 @@
#include <drogon/HttpRequest.h>
#include <trantor/utils/NonCopyable.h>
#include <trantor/net/EventLoop.h>
#include <cstddef>
#include <functional>
#include <memory>
#include <future>
@ -178,6 +179,12 @@ class DROGON_EXPORT HttpClient : public trantor::NonCopyable
*/
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
/// responding.
/**

View File

@ -19,6 +19,9 @@
#include <trantor/net/EventLoop.h>
#include <trantor/net/Resolver.h>
#include <trantor/net/TcpClient.h>
#include <cstddef>
#include <functional>
#include <future>
#include <list>
#include <mutex>
#include <queue>
@ -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<std::size_t> bufferSize;
loop_->queueInLoop(
[&] { bufferSize.set_value(requestsBuffer_.size()); });
return bufferSize.get_future().get();
}
}
private:
std::shared_ptr<trantor::TcpClient> tcpClientPtr_;
trantor::EventLoop *loop_;