Fray Engine
Loading...
Searching...
No Matches
fray::routing::NeighborMonitor< Key > Class Template Reference

Tracks neighbor liveness and health from what the router hears, and reports state transitions. Not thread safe: all calls and callbacks run on the router (engine message) thread. Never blocks or waits on a neighbor; the clock is injected so time-based rules (3s suspect/degraded, 10s down/unreachable) can be tested. It holds no readiness table itself; the owning router is the source of truth, read through the State callback. More...

#include <neighbor_monitor.hpp>

Classes

struct  QueueHealth
struct  HealthReport
struct  Neighbor

Public Types

using Clock = std::chrono::steady_clock
using Now = std::function<Clock::time_point()>
using Send = std::function<void(Key, MonitorCommand)>
using State = std::function<NeighborState(Key)>
using Changed = std::function<void(Key, NeighborState)>

Public Member Functions

 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 observe (Key neighbor, bool ready)
 Records that a message was heard from the neighbor and may move it to ready or degraded. Ignored for neighbors not started. A degraded neighbor stays degraded here; tick() recovers it.
void report (Key neighbor, HealthReport health)
 Stores a health report received from a neighbor and updates heartbeat and queue-progress timestamps. Ignored for neighbors not started.
void tick ()
 Periodic check (the owner calls it about once a second): applies the timing rules and sends probes/pings. Probing neighbors get a probe, or become unreachable after 10 seconds. Ready, suspect, degraded and down neighbors become down after 10 seconds of silence, suspect after 3, degraded if health looks bad, or ready again if a degraded one recovered, and each gets a ping. State changes are requested through the Changed callback.

Private Member Functions

void setState (Key neighbor, NeighborState state)
 Calls the Changed callback only if the router's current state differs from the requested one.

Static Private Member Functions

static bool hasQueue (const HealthReport &health)
 True if any queue in the report has a nonzero depth.
static bool isDegraded (const Neighbor &entry, Clock::time_point now)
 Decides whether a neighbor's health is bad at the given time.

Private Attributes

Send _send
State _state
Changed _changed
Now _now
std::map< Key, Neighbor > _neighbors

Detailed Description

template<class Key>
class fray::routing::NeighborMonitor< Key >

Tracks neighbor liveness and health from what the router hears, and reports state transitions. Not thread safe: all calls and callbacks run on the router (engine message) thread. Never blocks or waits on a neighbor; the clock is injected so time-based rules (3s suspect/degraded, 10s down/unreachable) can be tested. It holds no readiness table itself; the owning router is the source of truth, read through the State callback.

Template Parameters
KeyIdentifies a neighbor (ordered; for example ProcessKind).

Definition at line 26 of file neighbor_monitor.hpp.

Member Typedef Documentation

◆ Changed

template<class Key>
using fray::routing::NeighborMonitor< Key >::Changed = std::function<void(Key, NeighborState)>

Callback invoked when the monitor decides a neighbor should be in a different state.

Definition at line 38 of file neighbor_monitor.hpp.

◆ Clock

template<class Key>
using fray::routing::NeighborMonitor< Key >::Clock = std::chrono::steady_clock

Monotonic clock.

Definition at line 30 of file neighbor_monitor.hpp.

◆ Now

template<class Key>
using fray::routing::NeighborMonitor< Key >::Now = std::function<Clock::time_point()>

Callback returning the current time.

Definition at line 32 of file neighbor_monitor.hpp.

◆ Send

template<class Key>
using fray::routing::NeighborMonitor< Key >::Send = std::function<void(Key, MonitorCommand)>

Callback that transmits a probe or ping to a neighbor.

Definition at line 34 of file neighbor_monitor.hpp.

◆ State

template<class Key>
using fray::routing::NeighborMonitor< Key >::State = std::function<NeighborState(Key)>

Callback returning the router's current state for a neighbor.

Definition at line 36 of file neighbor_monitor.hpp.

Constructor & Destructor Documentation

◆ NeighborMonitor()

template<class Key>
fray::routing::NeighborMonitor< Key >::NeighborMonitor ( Send send,
State state,
Changed changed,
Now now = [] { return Clock::now(); } )
inline

Creates a monitor with no neighbors.

Parameters
sendTransmits a probe or ping to a neighbor.
stateReads the router's current state for a neighbor.
changedCalled when the monitor wants a neighbor moved to a new state; the owner applies it.
nowTime source; defaults to the steady clock.

Definition at line 61 of file neighbor_monitor.hpp.

Member Function Documentation

◆ hasQueue()

template<class Key>
bool fray::routing::NeighborMonitor< Key >::hasQueue ( const HealthReport & health)
inlinestaticprivate

True if any queue in the report has a nonzero depth.

Definition at line 176 of file neighbor_monitor.hpp.

◆ isDegraded()

template<class Key>
bool fray::routing::NeighborMonitor< Key >::isDegraded ( const Neighbor & entry,
Clock::time_point now )
inlinestaticprivate

Decides whether a neighbor's health is bad at the given time.

Parameters
entryNeighbor bookkeeping.
nowTime to evaluate at.
Returns
true if no report arrived 3s after readiness, or the report is older than 3s, the heartbeat has not changed for 3s, or queued work made no progress for 3s.

Definition at line 188 of file neighbor_monitor.hpp.

◆ observe()

template<class Key>
void fray::routing::NeighborMonitor< Key >::observe ( Key neighbor,
bool ready )
inline

Records that a message was heard from the neighbor and may move it to ready or degraded. Ignored for neighbors not started. A degraded neighbor stays degraded here; tick() recovers it.

Parameters
neighborNeighbor heard from.
readyTrue if the message announced readiness (a READY), which also starts the expectation of health reports.

Definition at line 86 of file neighbor_monitor.hpp.

◆ report()

template<class Key>
void fray::routing::NeighborMonitor< Key >::report ( Key neighbor,
HealthReport health )
inline

Stores a health report received from a neighbor and updates heartbeat and queue-progress timestamps. Ignored for neighbors not started.

Parameters
neighborNeighbor that reported.
healthThe report, moved into the monitor.

Definition at line 107 of file neighbor_monitor.hpp.

◆ setState()

template<class Key>
void fray::routing::NeighborMonitor< Key >::setState ( Key neighbor,
NeighborState state )
inlineprivate

Calls the Changed callback only if the router's current state differs from the requested one.

Parameters
neighborNeighbor to update.
stateDesired state.

Definition at line 203 of file neighbor_monitor.hpp.

◆ start()

template<class Key>
void fray::routing::NeighborMonitor< Key >::start ( Key neighbor)
inline

Begins monitoring a neighbor (restarting if already tracked) and requests the probing state.

Parameters
neighborNeighbor to watch; the next tick() sends probes until it answers or 10 seconds pass.

Definition at line 72 of file neighbor_monitor.hpp.

◆ tick()

template<class Key>
void fray::routing::NeighborMonitor< Key >::tick ( )
inline

Periodic check (the owner calls it about once a second): applies the timing rules and sends probes/pings. Probing neighbors get a probe, or become unreachable after 10 seconds. Ready, suspect, degraded and down neighbors become down after 10 seconds of silence, suspect after 3, degraded if health looks bad, or ready again if a degraded one recovered, and each gets a ping. State changes are requested through the Changed callback.

Definition at line 130 of file neighbor_monitor.hpp.

Member Data Documentation

◆ _changed

template<class Key>
Changed fray::routing::NeighborMonitor< Key >::_changed
private

Requests state changes.

Definition at line 214 of file neighbor_monitor.hpp.

◆ _neighbors

template<class Key>
std::map<Key, Neighbor> fray::routing::NeighborMonitor< Key >::_neighbors
private

Watched neighbors.

Definition at line 218 of file neighbor_monitor.hpp.

◆ _now

template<class Key>
Now fray::routing::NeighborMonitor< Key >::_now
private

Time source.

Definition at line 216 of file neighbor_monitor.hpp.

◆ _send

template<class Key>
Send fray::routing::NeighborMonitor< Key >::_send
private

Transmits probes and pings.

Definition at line 210 of file neighbor_monitor.hpp.

◆ _state

template<class Key>
State fray::routing::NeighborMonitor< Key >::_state
private

Reads the router's state for a neighbor.

Definition at line 212 of file neighbor_monitor.hpp.


The documentation for this class was generated from the following file: