Add async/resilience/reflection primitives + FSM v2 rewrite
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>
This commit is contained in:
1 parent
43b5a81504
commit
80293ee56f
93 files changed
+8500
-3312
No files matched your search
@@ -0,0 +1,82 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <functional>
|
||||
#include <mutex>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Resilience
|
||||
{
|
||||
|
||||
// ── TokenBucket
|
||||
// ─────────────────────────────────────────────────────────────── A
|
||||
// token-bucket rate limiter. The bucket holds up to `capacity` tokens and
|
||||
// refills continuously at `refill_per_sec` tokens per second.
|
||||
// try_acquire(n) consumes n tokens when available. Thread-safe; the clock
|
||||
// is injectable so the refill behaviour is deterministic in tests.
|
||||
|
||||
class TokenBucket
|
||||
{
|
||||
public:
|
||||
|
||||
using Clock =
|
||||
std::function<std::chrono::steady_clock::time_point()>;
|
||||
using TimePoint = std::chrono::steady_clock::time_point;
|
||||
|
||||
TokenBucket(double capacity, double refill_per_sec,
|
||||
Clock clock = default_clock())
|
||||
: capacity_{capacity}, rate_{refill_per_sec}, tokens_{capacity},
|
||||
clock_{std::move(clock)}, last_{clock_()}
|
||||
{
|
||||
}
|
||||
|
||||
// Consume `tokens` if the bucket (after refilling for elapsed time)
|
||||
// holds at least that many. Returns false without consuming
|
||||
// anything otherwise.
|
||||
[[nodiscard]] bool try_acquire(double tokens = 1.0)
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
refill();
|
||||
if (tokens_ >= tokens) {
|
||||
tokens_ -= tokens;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Current token count after refilling for elapsed time.
|
||||
[[nodiscard]] double available()
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
refill();
|
||||
return tokens_;
|
||||
}
|
||||
|
||||
[[nodiscard]] double capacity() const noexcept { return capacity_; }
|
||||
|
||||
private:
|
||||
|
||||
static Clock default_clock()
|
||||
{
|
||||
return [] { return std::chrono::steady_clock::now(); };
|
||||
}
|
||||
|
||||
void refill()
|
||||
{
|
||||
const auto now = clock_();
|
||||
const std::chrono::duration<double> elapsed = now - last_;
|
||||
last_ = now;
|
||||
tokens_ =
|
||||
std::min(capacity_, tokens_ + elapsed.count() * rate_);
|
||||
}
|
||||
|
||||
double capacity_;
|
||||
double rate_;
|
||||
double tokens_;
|
||||
Clock clock_;
|
||||
TimePoint last_;
|
||||
std::mutex mu_;
|
||||
};
|
||||
|
||||
} // namespace Void::Resilience
|
||||
Reference in new issue
Block a user