1#ifndef FRAY_NEIGHBOR_MONITOR_HPP
2#define FRAY_NEIGHBOR_MONITOR_HPP
30 using Clock = std::chrono::steady_clock;
32 using Now = std::function<Clock::time_point()>;
65 Now now = [] {
return Clock::now(); })
75 const auto now =
_now();
76 _neighbors[neighbor] = {now, now, std::nullopt, std::nullopt, now, now, now};
92 auto &entry = found->second;
93 entry.lastHeard =
_now();
94 if (
ready && !entry.healthExpectedSince) entry.healthExpectedSince = entry.lastHeard;
113 auto &entry = found->second;
114 const auto now =
_now();
115 if (!entry.health || entry.health->heartbeat != health.
heartbeat) entry.heartbeatChanged = now;
116 const bool queued =
hasQueue(health);
117 const bool wasQueued = entry.health &&
hasQueue(*entry.health);
118 if (!queued || !wasQueued || entry.health->processedCount != health.
processedCount)
119 entry.queueProgress = now;
120 entry.health = std::move(health);
121 entry.healthReceived = now;
132 using namespace std::chrono_literals;
133 const auto now =
_now();
146 const auto silence = now - entry.lastHeard;
179 for (
const auto &queue : health.
queues)
if (queue.depth != 0)
return true;
190 Clock::time_point now)
192 using namespace std::chrono_literals;
static bool hasQueue(const HealthReport &health)
True if any queue in the report has a nonzero depth.
std::map< Key, Neighbor > _neighbors
static bool isDegraded(const Neighbor &entry, Clock::time_point now)
Decides whether a neighbor's health is bad at the given time.
NeighborMonitor(Send send, State state, Changed changed, Now now=[] { return Clock::now();})
Creates a monitor with no neighbors.
void start(Key neighbor)
Begins monitoring a neighbor (restarting if already tracked) and requests the probing state.
void tick()
Periodic check (the owner calls it about once a second): applies the timing rules and sends probes/pi...
std::chrono::steady_clock Clock
std::function< Clock::time_point()> Now
std::function< void(Key, MonitorCommand)> Send
void report(Key neighbor, HealthReport health)
Stores a health report received from a neighbor and updates heartbeat and queue-progress timestamps....
void setState(Key neighbor, NeighborState state)
Calls the Changed callback only if the router's current state differs from the requested one.
std::function< NeighborState(Key)> State
std::function< void(Key, NeighborState)> Changed
void observe(Key neighbor, bool ready)
Records that a message was heard from the neighbor and may move it to ready or degraded....
NeighborState
Readiness of a neighbor process as seen by the router. unknown: nothing known yet; probing: readiness...
MonitorCommand
What the monitor asks its Send callback to transmit: probe = ask for readiness, ping = request a heal...
std::vector< QueueHealth > queues
std::optional< Clock::time_point > healthExpectedSince
Clock::time_point heartbeatChanged
std::optional< HealthReport > health
Clock::time_point queueProgress
Clock::time_point healthReceived
Clock::time_point lastHeard