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 {
25 public:
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,
32 Stats& stats, const Config& config);
33
34 virtual ~Controller() = default;
35
36 void Start();
37 void Stop();
38
39 void Step();
40
41 const std::string& GetName() const;
42
43 void SetEnabled(bool enabled);
44
45 protected:
46 virtual Limit Update(const v2::Sensor::Data& data) = 0;
47
48 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
49 std::optional<size_t> current_limit_;
50
51 private:
52 std::string_view LogFakeMode() const;
53
54 const std::string name_;
55 Sensor& sensor_;
56 congestion_control::Limiter& limiter_;
57 Stats& stats_;
58 const Config config_;
59 USERVER_NAMESPACE::utils::PeriodicTask periodic_;
60 std::atomic<bool> enabled_{true};
61};
62
63} // namespace congestion_control::v2
64
65USERVER_NAMESPACE_END