9#include <userver/congestion_control/config.hpp>
10#include <userver/congestion_control/limiter.hpp>
11#include <userver/congestion_control/sensor.hpp>
12#include <userver/dynamic_config/source.hpp>
13#include <userver/formats/json_fwd.hpp>
15USERVER_NAMESPACE_BEGIN
21 size_t times_with_overload{0};
22 size_t times_wo_overload{0};
24 bool is_overloaded{
false};
25 std::optional<size_t> current_limit;
27 size_t max_up_delta{1};
31 std::atomic<size_t> no_limit{0};
32 std::atomic<size_t> not_overload_no_pressure{0};
33 std::atomic<size_t> not_overload_pressure{0};
34 std::atomic<size_t> overload_no_pressure{0};
35 std::atomic<size_t> overload_pressure{0};
37 std::atomic<size_t> current_state{0};
38 std::atomic<std::chrono::seconds> last_overload_pressure{
39 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch()) -
40 std::chrono::hours(1)};
43class Controller
final {
45 Controller(std::string name, dynamic_config::Source config_source);
49 Limit GetLimit()
const;
51 Limit GetLimitRaw()
const;
53 void SetEnabled(
bool enabled);
55 bool IsEnabled()
const;
57 const Stats& GetStats()
const;
62 size_t CalcNewLimit(
const Sensor::Data& data,
const Policy& policy)
const;
64 static bool IsThresholdReached(
const Sensor::
Data& data,
int percent);
66 const std::string name_;
68 dynamic_config::Source config_source_;
70 std::atomic<
bool> is_enabled_;
78 Controller& controller;