9#include <unordered_map>
11USERVER_NAMESPACE_BEGIN
19class ResponseDataAccounter
final {
21 void StartRequest(size_t size,
22 std::chrono::steady_clock::time_point create_time);
24 void StopRequest(size_t size,
25 std::chrono::steady_clock::time_point create_time);
27 size_t GetCurrentLevel()
const {
return current_; }
29 size_t GetMaxLevel()
const {
return max_; }
31 void SetMaxLevel(size_t size) { max_ = size; }
33 std::chrono::milliseconds GetAvgRequestTime()
const;
36 std::atomic<size_t> current_{0};
37 std::atomic<size_t> max_{std::numeric_limits<size_t>::max()};
38 std::atomic<size_t> count_{0};
39 std::atomic<size_t> time_sum_{0};
45 explicit ResponseBase(ResponseDataAccounter& data_accounter);
48 virtual ~ResponseBase()
noexcept;
50 void SetData(std::string data);
51 const std::string& GetData()
const {
return data_; }
53 virtual bool IsBodyStreamed()
const = 0;
54 virtual bool WaitForHeadersEnd() = 0;
55 virtual void SetHeadersEnd() = 0;
60 void SetReady(std::chrono::steady_clock::time_point now);
61 virtual void SetSendFailed(
62 std::chrono::steady_clock::time_point failure_time);
63 bool IsLimitReached()
const;
65 bool IsReady()
const {
return is_ready_; }
66 bool IsSent()
const {
return is_sent_; }
67 size_t BytesSent()
const {
return bytes_sent_; }
68 std::chrono::steady_clock::time_point ReadyTime()
const {
71 std::chrono::steady_clock::time_point SentTime()
const {
return sent_time_; }
73 virtual void SendResponse(engine::
io::
RwBase& socket) = 0;
75 virtual void SetStatusServiceUnavailable() = 0;
76 virtual void SetStatusOk() = 0;
77 virtual void SetStatusNotFound() = 0;
81 void SetSent(std::size_t bytes_sent,
82 std::chrono::steady_clock::time_point sent_time);
87 Guard(ResponseDataAccounter& accounter,
88 std::chrono::steady_clock::time_point create_time, size_t size)
89 : accounter_(accounter), create_time_(create_time), size_(size) {
90 accounter_.StartRequest(size_, create_time_);
93 ~Guard() { accounter_.StopRequest(size_, create_time_); }
96 ResponseDataAccounter& accounter_;
97 std::chrono::steady_clock::time_point create_time_;
101 ResponseDataAccounter& accounter_;
102 std::optional<Guard> guard_;
104 std::chrono::steady_clock::time_point create_time_;
105 std::chrono::steady_clock::time_point ready_time_;
106 std::chrono::steady_clock::time_point sent_time_;
107 size_t bytes_sent_ = 0;
108 bool is_ready_ =
false;
109 bool is_sent_ =
false;