userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
controller.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/congestion_control/controller.hpp
4
/// @brief Congestion control controller and policy state
5
6
#
include
<
atomic
>
7
#
include
<
chrono
>
8
#
include
<
cstddef
>
9
#
include
<
optional
>
10
#
include
<
string
>
11
12
#
include
<
userver
/
congestion_control
/
limiter
.
hpp
>
13
#
include
<
userver
/
congestion_control
/
sensor
.
hpp
>
14
#
include
<
userver
/
dynamic_config
/
source
.
hpp
>
15
#
include
<
userver
/
formats
/
json_fwd
.
hpp
>
16
#
include
<
userver
/
utils
/
statistics
/
rate_counter
.
hpp
>
17
18
#
include
<
dynamic_config
/
variables
/
USERVER_RPS_CCONTROL
.
hpp
>
19
20
USERVER_NAMESPACE_BEGIN
21
22
/// Congestion Control
23
namespace
congestion_control
{
24
25
struct
PolicyState
{
26
size_t times_with_overload{0};
27
size_t times_wo_overload{0};
28
29
bool
is_overloaded{
false
};
30
std::optional<size_t> current_limit;
31
32
size_t max_up_delta{1};
33
};
34
35
struct
Stats
final
{
36
utils
::
statistics
::RateCounter no_limit{};
37
utils
::
statistics
::RateCounter not_overload_no_pressure{};
38
utils
::
statistics
::RateCounter not_overload_pressure{};
39
utils
::
statistics
::RateCounter overload_no_pressure{};
40
utils
::
statistics
::RateCounter overload_pressure{};
41
42
std::atomic<size_t> current_state{0};
43
std::atomic<std::chrono::seconds> last_overload_pressure{
44
std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now().time_since_epoch()) -
45
std::chrono::hours(1)
46
};
47
};
48
49
using
Policy = ::dynamic_config::userver_rps_ccontrol::VariableType;
50
51
class
Controller
final
{
52
public
:
53
Controller(std::string name, dynamic_config::Source config_source);
54
55
void
Feed(
const
Sensor
::
Data
&);
56
57
Limit
GetLimit()
const
;
58
59
Limit
GetLimitRaw()
const
;
60
61
void
SetEnabled(
bool
enabled);
62
63
bool
IsEnabled()
const
;
64
65
const
Stats& GetStats()
const
;
66
67
private
:
68
bool
IsOverloadedNow(
const
Sensor
::
Data
& data,
const
Policy& policy)
const
;
69
70
size_t CalcNewLimit(
const
Sensor
::
Data
& data,
const
Policy& policy)
const
;
71
72
static
bool
IsThresholdReached(
const
Sensor
::
Data
& data,
int
percent);
73
74
const
std::string name_;
75
Limit
limit_;
76
dynamic_config::Source config_source_;
77
PolicyState
state_;
78
std::atomic<
bool
> is_enabled_;
79
80
Stats stats_;
81
};
82
83
struct
ControllerInfo
{
84
Sensor
& sensor;
85
Limiter
& limiter;
86
Controller& controller;
87
};
88
89
}
// namespace congestion_control
90
91
USERVER_NAMESPACE_END
userver
congestion_control
controller.hpp
Generated on
for userver by
Doxygen
1.17.0