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 {
14 public:
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 {
34 public:
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 {
44 return static_cast<double>(timeouts) / (total ? total : 1);
45 }
46
47 std::string ToLogString() const;
48 };
49
50 virtual ~Sensor() = default;
51
52 virtual Data GetCurrent() = 0;
53};
54
55} // namespace v2
56
57} // namespace congestion_control
58
59USERVER_NAMESPACE_END