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 void AddAsSingleProducer(
Rate arg)
noexcept {
59 val_.store(val_.load(std::memory_order_relaxed) + arg.value,
60 std::memory_order_relaxed);
63 RateCounter& operator++()
noexcept {
64 val_.fetch_add(1, std::memory_order_relaxed);
68 Rate operator++(
int)
noexcept {
69 return Rate{val_.fetch_add(1, std::memory_order_relaxed)};
72 RateCounter& operator+=(
Rate arg)
noexcept {
73 val_.fetch_add(arg.value, std::memory_order_relaxed);
77 RateCounter& operator+=(
const RateCounter& arg)
noexcept {
83 static_assert(std::atomic<
Rate::ValueType>::is_always_lock_free);
85 std::atomic<
Rate::ValueType> val_{0};
88void DumpMetric(Writer& writer,
const RateCounter& value);
90void ResetMetric(RateCounter& value);