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 {
34 public:
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
52 private:
53 std::atomic<std::uint32_t> max_tokens_;
54 std::atomic<std::uint32_t> token_ratio_;
55 std::atomic<std::int32_t> token_count_;
56 std::atomic<bool> enabled_{false};
57
58 // rate of account oks
59 utils::statistics::RateCounter ok_rate_counter_;
60 // rate of account fails
61 utils::statistics::RateCounter fail_rate_counter_;
62
63 friend void DumpMetric(statistics::Writer& writer, const RetryBudget& budget);
64};
65
66RetryBudgetSettings Parse(const formats::json::Value& elem,
67 formats::parse::To<RetryBudgetSettings>);
68
69} // namespace utils
70
71USERVER_NAMESPACE_END