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
@@ -5,11 +5,13 @@
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
using Void::Coro::Task;
|
||||
using Void::Fsm::MachineOptions;
|
||||
using Void::Fsm::Step;
|
||||
using Void::Fsm::State;
|
||||
using Void::Fsm::StateKey;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
namespace
|
||||
@@ -20,8 +22,9 @@ namespace
|
||||
struct Stop {
|
||||
};
|
||||
|
||||
// A declarative machine: Idle reacts to events with guards/actions; on
|
||||
// timeout it bails out.
|
||||
// Imperative event handling: Idle loops, awaiting either Go or Stop. A Go
|
||||
// only advances when positive (the guard, now a plain `if`); Stop leaves to
|
||||
// Done. Internal handling = stay in the loop (no transition).
|
||||
class Gate : public Void::Fsm::Machine<Gate>
|
||||
{
|
||||
public:
|
||||
@@ -32,42 +35,46 @@ namespace
|
||||
int total = 0;
|
||||
std::vector<std::string> trace;
|
||||
|
||||
struct Idle : State {
|
||||
void transitions(TransitionBuilder& on)
|
||||
{
|
||||
// Guarded: only positive amounts advance.
|
||||
on.event<Go>()
|
||||
.guard(
|
||||
[](Gate&, const Go& g) { return g.amount > 0; })
|
||||
.action([](Gate& m, const Go& g) {
|
||||
m.total += g.amount;
|
||||
m.trace.push_back("go" +
|
||||
std::to_string(g.amount));
|
||||
})
|
||||
.template to<Idle>();
|
||||
on.event<Stop>().template to<Done>();
|
||||
on.after(10s).fail("idle timeout");
|
||||
}
|
||||
};
|
||||
struct States {
|
||||
struct Idle : State {
|
||||
Task<StateKey> run()
|
||||
{
|
||||
auto& m = self();
|
||||
for (;;) {
|
||||
auto ev = co_await await_any<Go, Stop>();
|
||||
if (std::holds_alternative<Stop>(ev)) {
|
||||
co_return next<Done>();
|
||||
}
|
||||
const Go g = std::get<Go>(ev);
|
||||
if (g.amount > 0) { // guard
|
||||
m.total += g.amount;
|
||||
m.trace.push_back(
|
||||
"go" + std::to_string(g.amount));
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// Imperative terminal state: reports the accumulated total.
|
||||
struct Done : State {
|
||||
Task<Step> run() { co_return succeed(self().total); }
|
||||
// Imperative terminal state: reports the accumulated total.
|
||||
struct Done : State {
|
||||
Task<StateKey> run()
|
||||
{
|
||||
co_return succeed(self().total);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
using States = Void::Fsm::States<Idle, Done>;
|
||||
|
||||
Gate() {} // own worker
|
||||
};
|
||||
} // namespace
|
||||
|
||||
// Mixes a reactive (declarative, guarded) state with an imperative one.
|
||||
// Imperative event loop with a guard and an accumulating action.
|
||||
Void::Testing::Test reactive_guard_and_action()
|
||||
{
|
||||
Gate fsm;
|
||||
fsm.start();
|
||||
fsm.post(Go{5});
|
||||
fsm.post(Go{-1}); // blocked by guard, dropped
|
||||
fsm.post(Go{-1}); // blocked by the guard, ignored
|
||||
fsm.post(Go{3});
|
||||
fsm.post(Stop{}); // -> Done -> succeed(total)
|
||||
|
||||
@@ -93,17 +100,18 @@ namespace
|
||||
|
||||
using Success = std::string;
|
||||
|
||||
struct Waiting : State {
|
||||
Task<Step> run()
|
||||
{
|
||||
auto w = co_await await_event_for<Wake>(15ms);
|
||||
if (w.has_value()) {
|
||||
co_return succeed(std::string{"woken"});
|
||||
}
|
||||
co_return succeed(std::string{"timed_out"});
|
||||
}
|
||||
struct States {
|
||||
struct Waiting : State {
|
||||
Task<StateKey> run()
|
||||
{
|
||||
auto w = co_await await_event_for<Wake>(15ms);
|
||||
if (w.has_value()) {
|
||||
co_return succeed(std::string{"woken"});
|
||||
}
|
||||
co_return succeed(std::string{"timed_out"});
|
||||
}
|
||||
};
|
||||
};
|
||||
using States = Void::Fsm::States<Waiting>;
|
||||
Sleeper() {} // own worker
|
||||
};
|
||||
} // namespace
|
||||
@@ -130,27 +138,29 @@ Void::Testing::Test await_event_for_receives_event()
|
||||
|
||||
namespace
|
||||
{
|
||||
// Reactive timeout transition (on.after().to<>).
|
||||
// Timeout transition expressed imperatively (await_event_for → branch).
|
||||
class Deadline : public Void::Fsm::Machine<Deadline>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct Pending : State {
|
||||
void transitions(TransitionBuilder& on)
|
||||
{
|
||||
on.event<Wake>().template to<Resolved>();
|
||||
on.after(15ms).template to<Expired>();
|
||||
}
|
||||
struct States {
|
||||
struct Pending : State {
|
||||
Task<StateKey> run()
|
||||
{
|
||||
auto w = co_await await_event_for<Wake>(15ms);
|
||||
co_return w.has_value() ? next<Resolved>()
|
||||
: next<Expired>();
|
||||
}
|
||||
};
|
||||
struct Resolved : State {
|
||||
Task<StateKey> run() { co_return succeed(1); }
|
||||
};
|
||||
struct Expired : State {
|
||||
Task<StateKey> run() { co_return succeed(2); }
|
||||
};
|
||||
};
|
||||
struct Resolved : State {
|
||||
Task<Step> run() { co_return succeed(1); }
|
||||
};
|
||||
struct Expired : State {
|
||||
Task<Step> run() { co_return succeed(2); }
|
||||
};
|
||||
using States = Void::Fsm::States<Pending, Resolved, Expired>;
|
||||
Deadline() {}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Reference in new issue
Block a user