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
@@ -4,11 +4,14 @@
|
||||
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
using Void::Coro::Task;
|
||||
using Void::Fsm::MachineOptions;
|
||||
using Void::Fsm::Step;
|
||||
using Void::Fsm::State;
|
||||
using Void::Fsm::StateKey;
|
||||
using Void::Fsm::Transition;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
@@ -30,30 +33,30 @@ namespace
|
||||
int attempts = 0;
|
||||
int retries; // how many tries with_retry gets
|
||||
|
||||
struct Try : State {
|
||||
Task<Step> run()
|
||||
{
|
||||
auto& m = self();
|
||||
const bool ok = co_await with_retry(
|
||||
m.retries, 1ms, [&m]() -> Task<bool> {
|
||||
++m.attempts;
|
||||
co_return m.attempts > m.fail_n;
|
||||
});
|
||||
co_return next_or<Fallback>(
|
||||
ok ? succeed(std::string{"ok"})
|
||||
: fail("exhausted"));
|
||||
}
|
||||
};
|
||||
struct States {
|
||||
struct Try : State {
|
||||
Task<StateKey> run()
|
||||
{
|
||||
auto& m = self();
|
||||
const bool ok = co_await with_retry(
|
||||
m.retries, 1ms, [&m]() -> Task<bool> {
|
||||
++m.attempts;
|
||||
co_return m.attempts > m.fail_n;
|
||||
});
|
||||
co_return next_or<Fallback>(
|
||||
ok ? succeed(std::string{"ok"})
|
||||
: fail("exhausted"));
|
||||
}
|
||||
};
|
||||
|
||||
struct Fallback : State {
|
||||
Task<Step> run()
|
||||
{
|
||||
co_return succeed(std::string{"fallback"});
|
||||
}
|
||||
struct Fallback : State {
|
||||
Task<StateKey> run()
|
||||
{
|
||||
co_return succeed(std::string{"fallback"});
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
using States = Void::Fsm::States<Try, Fallback>;
|
||||
|
||||
explicit Flaky(int fails, int tries) : fail_n(fails), retries(tries)
|
||||
{
|
||||
} // own worker
|
||||
@@ -91,13 +94,14 @@ namespace
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct A : State {
|
||||
Task<Step> run() { co_return next<B>(); }
|
||||
struct States {
|
||||
struct A : State {
|
||||
Task<StateKey> run() { co_return next<B>(); }
|
||||
};
|
||||
struct B : State {
|
||||
Task<StateKey> run() { co_return succeed(1); }
|
||||
};
|
||||
};
|
||||
struct B : State {
|
||||
Task<Step> run() { co_return succeed(1); }
|
||||
};
|
||||
using States = Void::Fsm::States<A, B>;
|
||||
Pipe()
|
||||
: Void::Fsm::Machine<Pipe>(MachineOptions{.own_worker = false})
|
||||
{
|
||||
@@ -149,63 +153,59 @@ namespace
|
||||
struct Cancel {
|
||||
};
|
||||
|
||||
// Imperative event handling: Idle awaits Go or Cancel and routes.
|
||||
class Flow : public Void::Fsm::Machine<Flow>
|
||||
{
|
||||
public:
|
||||
|
||||
using Success = int;
|
||||
|
||||
struct Idle : State {
|
||||
void transitions(TransitionBuilder& on)
|
||||
{
|
||||
on.event<Go>()
|
||||
.guard([](Flow&, const Go&) { return true; })
|
||||
.template to<Active>();
|
||||
on.event<Cancel>().template to<Done>();
|
||||
on.after(1h).template to<Done>();
|
||||
}
|
||||
struct States {
|
||||
struct Idle : State {
|
||||
Task<StateKey> run()
|
||||
{
|
||||
auto ev = co_await await_any<Go, Cancel>();
|
||||
co_return std::holds_alternative<Go>(ev)
|
||||
? next<Active>()
|
||||
: next<Done>();
|
||||
}
|
||||
};
|
||||
struct Active : State {
|
||||
Task<StateKey> run() { co_return next<Done>(); }
|
||||
};
|
||||
struct Done : State {
|
||||
Task<StateKey> run() { co_return succeed(0); }
|
||||
};
|
||||
};
|
||||
struct Active : State {
|
||||
Task<Step> run() { co_return next<Done>(); }
|
||||
};
|
||||
struct Done : State {
|
||||
Task<Step> run() { co_return succeed(0); }
|
||||
};
|
||||
using States = Void::Fsm::States<Idle, Active, Done>;
|
||||
Flow() {}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
Void::Testing::Test describe_lists_states_and_reactive_edges()
|
||||
// describe() is node-level: behaviour (and therefore transitions) is chosen
|
||||
// imperatively inside each state's entry, so edges are not statically knowable.
|
||||
Void::Testing::Test describe_lists_states_node_level()
|
||||
{
|
||||
Flow fsm;
|
||||
const auto states = fsm.describe();
|
||||
Void::Testing::expect_eq(states.size(), std::size_t{3});
|
||||
|
||||
// Idle is reactive + initial, with Go/Cancel/timeout edges.
|
||||
const auto& idle = states[0];
|
||||
Void::Testing::expect_eq(idle.name, std::string{"Idle"});
|
||||
Void::Testing::expect(idle.reactive);
|
||||
Void::Testing::expect(idle.initial);
|
||||
Void::Testing::expect_eq(idle.edges.size(), std::size_t{3});
|
||||
Void::Testing::expect_eq(idle.edges[0].event, std::string{"Go"});
|
||||
Void::Testing::expect_eq(idle.edges[0].target, std::string{"Active"});
|
||||
Void::Testing::expect(idle.edges[0].guarded);
|
||||
Void::Testing::expect_eq(idle.edges[2].event, std::string{"(timeout)"});
|
||||
|
||||
// Active is imperative → no declarable edges.
|
||||
Void::Testing::expect(!states[1].reactive);
|
||||
Void::Testing::expect(states[1].edges.empty());
|
||||
// Registration order; Idle is the initial state.
|
||||
Void::Testing::expect_eq(states[0].name, std::string{"Idle"});
|
||||
Void::Testing::expect(states[0].initial);
|
||||
Void::Testing::expect_eq(states[1].name, std::string{"Active"});
|
||||
Void::Testing::expect(!states[1].initial);
|
||||
Void::Testing::expect_eq(states[2].name, std::string{"Done"});
|
||||
}
|
||||
|
||||
Void::Testing::Test to_mermaid_contains_edges()
|
||||
Void::Testing::Test to_mermaid_lists_nodes_and_initial()
|
||||
{
|
||||
Flow fsm;
|
||||
const auto diagram = fsm.to_mermaid();
|
||||
std::cout << diagram << std::endl;
|
||||
Void::Testing::expect(diagram.find("stateDiagram-v2") != std::string::npos);
|
||||
Void::Testing::expect(diagram.find("[*] --> Idle") != std::string::npos);
|
||||
Void::Testing::expect(diagram.find("Idle --> Active : Go [guard]") !=
|
||||
std::string::npos);
|
||||
Void::Testing::expect(diagram.find("Idle --> Done : Cancel") !=
|
||||
std::string::npos);
|
||||
// One node per registered state.
|
||||
Void::Testing::expect(diagram.find("\n Idle\n") != std::string::npos);
|
||||
Void::Testing::expect(diagram.find("\n Active\n") != std::string::npos);
|
||||
Void::Testing::expect(diagram.find("\n Done\n") != std::string::npos);
|
||||
}
|
||||
Reference in new issue
Block a user