userver: userver/utils/retry_budget.hpp Source File
Loading...
Searching...
No Matches
retry_budget.hpp
Go to the documentation of this file.
1#pragma once
2
5
6#include <atomic>
7#include <cstdint>
8
13
14USERVER_NAMESPACE_BEGIN
15
16namespace utils {
17
18namespace statistics {
19class Writer;
20}
21
22struct 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
34class RetryBudget final {
35public:
36 RetryBudget();
37 explicit RetryBudget(const RetryBudgetSettings& settings);
38
40 void AccountOk() noexcept;
41
43 void AccountFail() noexcept;
44
47 bool CanRetry() const;
48
51 void SetSettings(const RetryBudgetSettings& settings);
52
53private:
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
68
69RetryBudgetSettings Parse(const yaml_config::Value& elem, formats::parse::To<RetryBudgetSettings>);
70
71} // namespace utils
72
73USERVER_NAMESPACE_END