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>(
40 std::chrono::steady_clock::now().time_since_epoch()) -
41 std::chrono::hours(1)};
44class Controller
final {
46 Controller(std::string name, dynamic_config::Source config_source);
50 Limit GetLimit()
const;
52 Limit GetLimitRaw()
const;
54 void SetEnabled(
bool enabled);
56 bool IsEnabled()
const;
58 const Stats& GetStats()
const;
63 size_t CalcNewLimit(
const Sensor::Data& data,
const Policy& policy)
const;
65 static bool IsThresholdReached(
const Sensor::
Data& data,
int percent);
67 const std::string name_;
69 dynamic_config::Source config_source_;
71 std::atomic<
bool> is_enabled_;
79 Controller& controller;