8#include <userver/utils/statistics/fwd.hpp>
9#include <userver/utils/statistics/rate.hpp>
11USERVER_NAMESPACE_BEGIN
13namespace utils::statistics {
19class RateCounter
final {
21 using ValueType =
Rate;
23 constexpr RateCounter()
noexcept =
default;
24 constexpr explicit RateCounter(
Rate desired)
noexcept : val_(desired.value) {}
25 constexpr explicit RateCounter(
Rate::ValueType desired)
noexcept
28 RateCounter(
const RateCounter& other)
noexcept : val_(other.Load().value) {}
30 RateCounter& operator=(
const RateCounter& other)
noexcept {
31 if (
this == &other)
return *
this;
37 RateCounter& operator=(
Rate desired)
noexcept {
42 void Store(
Rate desired,
43 std::memory_order order = std::memory_order_relaxed)
noexcept {
44 val_.store(desired.value, order);
47 Rate Load()
const noexcept {
48 return Rate{val_.load(std::memory_order_relaxed)};
52 std::memory_order order = std::memory_order_relaxed)
noexcept {
53 val_.fetch_add(arg.value, order);
56 RateCounter& operator++()
noexcept {
57 val_.fetch_add(1, std::memory_order_relaxed);
61 Rate operator++(
int)
noexcept {
62 return Rate{val_.fetch_add(1, std::memory_order_relaxed)};
65 RateCounter& operator+=(
Rate arg)
noexcept {
66 val_.fetch_add(arg.value, std::memory_order_relaxed);
70 RateCounter& operator+=(RateCounter arg)
noexcept {
71 return *
this += arg.Load();
75 static_assert(std::atomic<
Rate::ValueType>::is_always_lock_free);
77 std::atomic<
Rate::ValueType> val_{0};
80void DumpMetric(Writer& writer,
const RateCounter& value);
82void ResetMetric(RateCounter& value);