userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
retry_budget.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/utils/retry_budget.hpp
4
/// @brief @copybrief utils::RetryBudget
5
6
#
include
<
atomic
>
7
#
include
<
cstdint
>
8
9
#
include
<
userver
/
formats
/
json_fwd
.
hpp
>
10
#
include
<
userver
/
formats
/
parse
/
to
.
hpp
>
11
#
include
<
userver
/
utils
/
statistics
/
rate_counter
.
hpp
>
12
#
include
<
userver
/
yaml_config
/
yaml_config
.
hpp
>
13
14
USERVER_NAMESPACE_BEGIN
15
16
namespace
utils
{
17
18
namespace
statistics
{
19
class
Writer;
20
}
21
22
struct
RetryBudgetSettings
final
{
23
// Maximum number of tokens in the budget.
24
float
max_tokens{100.0f};
25
// The number of tokens by which the budget will be increased in case of a
26
// successful request.
27
float
token_ratio{0.1f};
28
// Enable/disable retry budget.
29
bool
enabled{
true
};
30
};
31
32
/// Class implements the same logic described in
33
/// https://github.com/grpc/proposal/blob/master/A6-client-retries.md#throttling-configuration
34
class
RetryBudget
final
{
35
public
:
36
RetryBudget();
37
explicit
RetryBudget(
const
RetryBudgetSettings& settings);
38
39
/// Call after a request succeeds.
40
void
AccountOk
()
noexcept
;
41
42
/// Call after a request fails.
43
void
AccountFail
()
noexcept
;
44
45
/// @brief Call before attempting a retry (but not before the initial
46
/// request).
47
bool
CanRetry
()
const
;
48
49
/// Thread-safe relative to AccountOk/AccountFail/CanRetry.
50
/// Not thread-safe relative other SetSettings method calls.
51
void
SetSettings
(
const
RetryBudgetSettings& settings);
52
53
private
:
54
friend
void
DumpMetric(
statistics
::Writer& writer,
const
RetryBudget& budget);
55
56
std::atomic<std::uint32_t> max_tokens_;
57
std::atomic<std::uint32_t> token_ratio_;
58
std::atomic<std::int32_t> token_count_;
59
std::atomic<
bool
> enabled_{
false
};
60
61
// rate of account oks
62
utils
::
statistics
::RateCounter ok_rate_counter_;
63
// rate of account fails
64
utils
::
statistics
::RateCounter fail_rate_counter_;
65
};
66
67
RetryBudgetSettings Parse(
const
formats::
json
::
Value
& elem, formats::
parse
::
To
<RetryBudgetSettings>);
68
69
RetryBudgetSettings Parse(
const
yaml_config::Value& elem, formats::
parse
::
To
<RetryBudgetSettings>);
70
71
}
// namespace utils
72
73
USERVER_NAMESPACE_END
userver
utils
retry_budget.hpp
Generated on
for userver by
Doxygen
1.17.0