A round of new universal capabilities (all generic — templated/reflection based, no app coupling), verified by the test suite (1103 passing): Data reflection: - Reflect.hpp now (de)serializes chrono::duration, chrono::time_point, std::variant (tagged), std::array, and non-string-keyed std::map; Meta.hpp gains is_time_point_v et al. Async: - EventBus gains Mode::Async (worker-thread dispatch + flush()). - ThreadPool gains a bounded queue, try_submit(), and rejection stats. New modules / containers: - Resilience: TokenBucket, KeyedRateLimiter<Key>, CircuitBreaker, ResourceMonitor. - Containers: BufferPool<T> (size-class buffer reuse) and Cache<K,V> (per-entry TTL, LRU/LFU/FIFO eviction, hit/miss stats). - System: process/CPU/memory metrics (Metrics.hpp) and a streaming Process API. Plus FSM v2 (typed Success/Failure, sub-machines, parallel regions, history states, deferred events), Configuration/Logging reflection helpers, and the accompanying test suites. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
118 lines
3.0 KiB
C++
118 lines
3.0 KiB
C++
#pragma once
|
|
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <utility>
|
|
|
|
namespace Void::Resilience
|
|
{
|
|
|
|
// ── ResourceMonitor
|
|
// ─────────────────────────────────────────────────────────── Tracks usage
|
|
// of a bounded resource (memory budget, in-flight requests, queue depth,
|
|
// ...) against a limit, with hysteresis watermark callbacks.
|
|
//
|
|
// try_acquire() refuses to exceed the limit. Crossing the high watermark
|
|
// upward fires on_high_watermark once; dropping back to the low watermark
|
|
// fires on_low_watermark once (the gap prevents callback flapping).
|
|
// Callbacks run outside the internal lock.
|
|
|
|
class ResourceMonitor
|
|
{
|
|
public:
|
|
|
|
using Callback = std::function<void(std::uint64_t usage)>;
|
|
|
|
// high/low are fractions of `limit` in [0, 1]; high should exceed
|
|
// low.
|
|
explicit ResourceMonitor(std::uint64_t limit, double high = 0.9,
|
|
double low = 0.7)
|
|
: limit_{limit}, high_{static_cast<std::uint64_t>(
|
|
static_cast<double>(limit) * high)},
|
|
low_{static_cast<std::uint64_t>(static_cast<double>(limit) *
|
|
low)}
|
|
{
|
|
}
|
|
|
|
void on_high_watermark(Callback cb) { on_high_ = std::move(cb); }
|
|
void on_low_watermark(Callback cb) { on_low_ = std::move(cb); }
|
|
|
|
// Reserve `amount`. Returns false (reserving nothing) if it would
|
|
// exceed the limit. May fire the high-watermark callback when
|
|
// crossing upward.
|
|
[[nodiscard]] bool try_acquire(std::uint64_t amount = 1)
|
|
{
|
|
Callback fire;
|
|
std::uint64_t at = 0;
|
|
{
|
|
std::lock_guard lock{mu_};
|
|
if (usage_ + amount > limit_) {
|
|
return false;
|
|
}
|
|
usage_ += amount;
|
|
if (!alarmed_ && usage_ >= high_) {
|
|
alarmed_ = true;
|
|
fire = on_high_;
|
|
at = usage_;
|
|
}
|
|
}
|
|
if (fire) {
|
|
fire(at);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Release `amount` (clamped at zero). May fire the low-watermark
|
|
// callback when dropping back through the low threshold.
|
|
void release(std::uint64_t amount = 1)
|
|
{
|
|
Callback fire;
|
|
std::uint64_t at = 0;
|
|
{
|
|
std::lock_guard lock{mu_};
|
|
usage_ = (amount >= usage_) ? 0 : usage_ - amount;
|
|
if (alarmed_ && usage_ <= low_) {
|
|
alarmed_ = false;
|
|
fire = on_low_;
|
|
at = usage_;
|
|
}
|
|
}
|
|
if (fire) {
|
|
fire(at);
|
|
}
|
|
}
|
|
|
|
[[nodiscard]] std::uint64_t usage() const
|
|
{
|
|
std::lock_guard lock{mu_};
|
|
return usage_;
|
|
}
|
|
|
|
[[nodiscard]] std::uint64_t limit() const noexcept
|
|
{
|
|
return limit_;
|
|
}
|
|
|
|
[[nodiscard]] double utilization() const
|
|
{
|
|
std::lock_guard lock{mu_};
|
|
return limit_ == 0 ? 0.0
|
|
: static_cast<double>(usage_) /
|
|
static_cast<double>(limit_);
|
|
}
|
|
|
|
private:
|
|
|
|
std::uint64_t limit_;
|
|
std::uint64_t high_;
|
|
std::uint64_t low_;
|
|
std::uint64_t usage_ = 0;
|
|
bool alarmed_ = false;
|
|
Callback on_high_;
|
|
Callback on_low_;
|
|
mutable std::mutex mu_;
|
|
};
|
|
|
|
} // namespace Void::Resilience
|