userver: userver/congestion_control/sensor.hpp Source File
Loading...
Searching...
No Matches
sensor.hpp
1#pragma once
2
3#include <chrono>
4#include <cstdint>
5#include <string>
6
7USERVER_NAMESPACE_BEGIN
8
9namespace congestion_control {
10
11/// Abstract sensor that fetches counters. These counters will be used
12/// to forecast saturation levels and limit current load.
13class Sensor {
14public:
15 struct Data {
16 std::uint64_t current_load{0};
17 std::uint64_t overload_events_count{0};
18 std::uint64_t no_overload_events_count{0};
19 std::chrono::steady_clock::time_point tp;
20
21 double GetLoadPercent() const;
22 };
23
24 virtual ~Sensor() = default;
25
26 /// @brief Fetch current counters
27 /// @note Can be called both from coroutine and non-coroutine context
28 virtual Data FetchCurrent() = 0;
29};
30
31namespace v2 {
32
33class Sensor {
34public:
35 struct Data {
36 std::size_t total{0};
37 std::size_t timeouts{0};
38
39 std::size_t timings_avg_ms{0};
40
41 std::size_t current_load{0};
42
43 double GetRate() const { return static_cast<double>(timeouts) / (total ? total : 1); }
44
45 std::string ToLogString() const;
46 };
47
48 virtual ~Sensor() = default;
49
50 virtual Data GetCurrent() = 0;
51};
52
53} // namespace v2
54
55} // namespace congestion_control
56
57USERVER_NAMESPACE_END