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 : val_(desired) {}
29 RateCounter(
const RateCounter& other)
noexcept : val_(other.Load().value) {}
31 RateCounter& operator=(
const RateCounter& other)
noexcept {
32 if (
this == &other)
return *
this;
38 RateCounter& operator=(
Rate desired)
noexcept {
43 void Store(
Rate desired, std::memory_order order = std::memory_order_relaxed)
noexcept {
44 val_.store(desired.value, order);
47 Rate Load()
const noexcept {
return Rate{val_.load(std::memory_order_relaxed)}; }
49 void Add(
Rate arg, std::memory_order order = std::memory_order_relaxed)
noexcept {
50 val_.fetch_add(arg.value, order);
53 void AddAsSingleProducer(
Rate arg)
noexcept {
54 val_.store(val_.load(std::memory_order_relaxed) + arg.value, std::memory_order_relaxed);
57 RateCounter& operator++()
noexcept {
58 val_.fetch_add(1, std::memory_order_relaxed);
62 Rate operator++(
int)
noexcept {
return Rate{val_.fetch_add(1, std::memory_order_relaxed)}; }
64 RateCounter& operator+=(
Rate arg)
noexcept {
65 val_.fetch_add(arg.value, std::memory_order_relaxed);
69 RateCounter& operator+=(
const RateCounter& arg)
noexcept {
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);