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
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
13USERVER_NAMESPACE_BEGIN
14
15namespace utils {
16
17namespace statistics {
18class Writer;
19}
20
21struct RetryBudgetSettings final {
22 // Maximum number of tokens in the budget.
23 float max_tokens{100.0f};
24 // The number of tokens by which the budget will be increased in case of a
25 // successful request.
26 float token_ratio{0.1f};
27 // Enable/disable retry budget.
28 bool enabled{true};
29};
30
31/// Class implements the same logic described in
32/// https://github.com/grpc/proposal/blob/master/A6-client-retries.md#throttling-configuration
33class RetryBudget final {
34public:
35 RetryBudget();
36 explicit RetryBudget(const RetryBudgetSettings& settings);
37
38 /// Call after a request succeeds.
39 void AccountOk() noexcept;
40
41 /// Call after a request fails.
42 void AccountFail() noexcept;
43
44 /// @brief Call before attempting a retry (but not before the initial
45 /// request).
46 bool CanRetry() const;
47
48 /// Thread-safe relative to AccountOk/AccountFail/CanRetry.
49 /// Not thread-safe relative other SetSettings method calls.
50 void SetSettings(const RetryBudgetSettings& settings);
51
52private:
53 friend void DumpMetric(statistics::Writer& writer, const RetryBudget& budget);
54
55 std::atomic<std::uint32_t> max_tokens_;
56 std::atomic<std::uint32_t> token_ratio_;
57 std::atomic<std::int32_t> token_count_;
58 std::atomic<bool> enabled_{false};
59
60 // rate of account oks
61 utils::statistics::RateCounter ok_rate_counter_;
62 // rate of account fails
63 utils::statistics::RateCounter fail_rate_counter_;
64};
65
66RetryBudgetSettings Parse(const formats::json::Value& elem, formats::parse::To<RetryBudgetSettings>);
67
68} // namespace utils
69
70USERVER_NAMESPACE_END