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,123 @@
|
||||
#pragma once
|
||||
|
||||
#include <Void/Resilience/RateLimiter.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::Resilience
|
||||
{
|
||||
|
||||
// ── KeyedRateLimiter<Key> ───────────────────────────────────────────────
|
||||
// A rate limiter that maintains an independent TokenBucket per distinct
|
||||
// Key. Every key shares the same capacity + refill rate; buckets are
|
||||
// created lazily on first use and can be reclaimed with cleanup(). Thread-
|
||||
// safe. Generic over the key type (e.g. a client id, IP, or user name).
|
||||
|
||||
template <typename Key, typename Hash = std::hash<Key>>
|
||||
class KeyedRateLimiter
|
||||
{
|
||||
public:
|
||||
|
||||
using Clock = TokenBucket::Clock;
|
||||
|
||||
// `capacity` is the burst size; `refill_per_sec` the sustained rate.
|
||||
// The clock is shared with every bucket so tests stay deterministic.
|
||||
explicit KeyedRateLimiter(double capacity, double refill_per_sec,
|
||||
Clock clock = default_clock())
|
||||
: capacity_{capacity}, rate_{refill_per_sec},
|
||||
clock_{std::move(clock)}
|
||||
{
|
||||
}
|
||||
|
||||
// Consume `tokens` for `key`. Returns false (without consuming) when
|
||||
// the key's bucket is exhausted.
|
||||
[[nodiscard]] bool allow(const Key& key, double tokens = 1.0)
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
return entry(key).bucket->try_acquire(tokens);
|
||||
}
|
||||
|
||||
// Tokens currently available to `key` (creates the bucket if new).
|
||||
[[nodiscard]] double remaining(const Key& key)
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
return entry(key).bucket->available();
|
||||
}
|
||||
|
||||
// Forget a key's bucket (its next use starts full).
|
||||
void reset(const Key& key)
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
map_.erase(key);
|
||||
}
|
||||
|
||||
// Drop buckets not touched within `max_age`. Returns how many were
|
||||
// removed. Call periodically to bound memory for churny key spaces.
|
||||
std::size_t cleanup(std::chrono::steady_clock::duration max_age)
|
||||
{
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
std::lock_guard lock{mu_};
|
||||
std::size_t removed = 0;
|
||||
for (auto it = map_.begin(); it != map_.end();) {
|
||||
if (now - it->second.last_seen >= max_age) {
|
||||
it = map_.erase(it);
|
||||
++removed;
|
||||
} else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
// Number of keys currently tracked.
|
||||
[[nodiscard]] std::size_t tracked() const
|
||||
{
|
||||
std::lock_guard lock{mu_};
|
||||
return map_.size();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
static Clock default_clock()
|
||||
{
|
||||
return [] { return std::chrono::steady_clock::now(); };
|
||||
}
|
||||
|
||||
struct Entry {
|
||||
// TokenBucket owns a std::mutex (non-movable), so hold it by
|
||||
// pointer to keep Entry movable for the hash map.
|
||||
std::unique_ptr<TokenBucket> bucket;
|
||||
std::chrono::steady_clock::time_point last_seen;
|
||||
};
|
||||
|
||||
Entry& entry(const Key& key)
|
||||
{
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
auto it = map_.find(key);
|
||||
if (it == map_.end()) {
|
||||
it = map_
|
||||
.emplace(key,
|
||||
Entry{std::make_unique<TokenBucket>(
|
||||
capacity_, rate_, clock_),
|
||||
now})
|
||||
.first;
|
||||
} else {
|
||||
it->second.last_seen = now;
|
||||
}
|
||||
return it->second;
|
||||
}
|
||||
|
||||
double capacity_;
|
||||
double rate_;
|
||||
Clock clock_;
|
||||
mutable std::mutex mu_;
|
||||
std::unordered_map<Key, Entry, Hash> map_;
|
||||
};
|
||||
|
||||
} // namespace Void::Resilience
|
||||
Reference in new issue
Block a user