userver: userver/congestion_control/controllers/v2.hpp Source File
Loading...
Searching...
No Matches
v2.hpp
1#pragma once
2
3#include <atomic>
4#include <optional>
5
6#include <userver/congestion_control/limiter.hpp>
7#include <userver/congestion_control/sensor.hpp>
8#include <userver/utils/periodic_task.hpp>
9#include <userver/utils/statistics/fwd.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace congestion_control::v2 {
14
15struct Stats {
16 std::atomic<bool> is_enabled{false};
17 std::atomic<bool> is_fake_mode{false};
18 std::atomic<int64_t> current_limit{0};
19 std::atomic<int64_t> enabled_epochs{0};
20};
21
22void DumpMetric(utils::statistics::Writer& writer, const Stats& stats);
23
24class Controller {
25public:
26 struct Config {
27 bool fake_mode{false};
28 bool enabled{true};
29 };
30
31 Controller(const std::string& name, v2::Sensor& sensor, Limiter& limiter, Stats& stats, const Config& config);
32
33 virtual ~Controller() = default;
34
35 void Start();
36 void Stop();
37
38 void Step();
39
40 const std::string& GetName() const;
41
42 void SetEnabled(bool enabled);
43
44protected:
45 virtual Limit Update(const v2::Sensor::Data& data) = 0;
46
47 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
48 std::optional<size_t> current_limit_;
49
50private:
51 std::string_view LogFakeMode() const;
52
53 const std::string name_;
54 Sensor& sensor_;
55 congestion_control::Limiter& limiter_;
56 Stats& stats_;
57 const Config config_;
58 USERVER_NAMESPACE::utils::PeriodicTask periodic_;
59 std::atomic<bool> enabled_{true};
60};
61
62} // namespace congestion_control::v2
63
64USERVER_NAMESPACE_END