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;
25 constexpr explicit RateCounter(
Rate desired)
noexcept : val_(desired.value) {}
27 constexpr explicit RateCounter(
Rate::ValueType desired)
noexcept
30 RateCounter(
const RateCounter& other)
noexcept : val_(other.Load().value) {}
32 RateCounter& operator=(
const RateCounter& other)
noexcept {
33 if (
this == &other)
return *
this;
39 RateCounter& operator=(
Rate desired)
noexcept {
44 void Store(
Rate desired,
45 std::memory_order order = std::memory_order_relaxed)
noexcept {
46 val_.store(desired.value, order);
49 Rate Load()
const noexcept {
50 return Rate{val_.load(std::memory_order_relaxed)};
54 std::memory_order order = std::memory_order_relaxed)
noexcept {
55 val_.fetch_add(arg.value, order);
58 RateCounter& operator++()
noexcept {
59 val_.fetch_add(1, std::memory_order_relaxed);
63 Rate operator++(
int)
noexcept {
64 return Rate{val_.fetch_add(1, std::memory_order_relaxed)};
67 RateCounter& operator+=(
Rate arg)
noexcept {
68 val_.fetch_add(arg.value, std::memory_order_relaxed);
72 RateCounter& operator+=(
const RateCounter& arg)
noexcept {
78 static_assert(std::atomic<
Rate::ValueType>::is_always_lock_free);
80 std::atomic<
Rate::ValueType> val_{0};
83void DumpMetric(Writer& writer,
const RateCounter& value);
85void ResetMetric(RateCounter& value);