3#ifndef _FRAY_OUTGOING_ROUTER_HPP_
4#define _FRAY_OUTGOING_ROUTER_HPP_
36 template <
class Key,
class Frame>
41 using Clock = std::chrono::steady_clock;
43 using Deliver = std::function<void(Key,
const Frame &)>;
45 using Now = std::function<Clock::time_point()>;
64 Now now = [] {
return Clock::now(); })
75 const auto found =
_states.find(neighbor);
94 if (
state(neighbor) == value)
return false;
107 const auto found =
_queues.find(neighbor);
108 return found ==
_queues.end() ? 0 : found->second.size();
117 std::vector<QueueHealth> result;
118 const auto now =
_now();
119 for (
const auto &[neighbor, queue] :
_queues)
121 if (queue.empty())
continue;
122 const auto age = std::chrono::duration_cast<std::chrono::milliseconds>(now - queue.front().queuedAt);
123 result.push_back({neighbor, queue.size(),
static_cast<uint64_t
>(age.count() < 0 ? 0 : age.count())});
140 const auto found =
_queues.find(neighbor);
143 auto waiting = std::move(found->second);
145 const auto now =
_now();
146 for (
auto &entry : waiting)
147 if (!entry.expires || *entry.expires > now)
161 const bool wasReady =
isReady(neighbor);
174 const auto found =
_queues.find(neighbor);
177 const size_t discarded = found->second.size();
201 const auto now =
_now();
202 Entry entry{std::move(frame), now, std::nullopt};
204 entry.
expires = now + std::chrono::milliseconds(ttlMs);
205 _queues[neighbor].push_back(std::move(entry));
215 const auto now =
_now();
219 auto &entries = queue->second;
220 for (
auto entry = entries.begin(); entry != entries.end();)
222 if (entry->expires && *entry->expires <= now)
224 entry = entries.erase(entry);
230 queue = entries.empty() ?
_queues.erase(queue) : std::next(queue);
bool setState(Key neighbor, NeighborState value)
Records a neighbor's state. Setting ready goes through markReady, which also delivers waiting message...
std::function< Clock::time_point()> Now
bool markReady(Key neighbor)
Marks the neighbor ready and delivers its waiting messages in order, skipping any that expired....
size_t queued(Key neighbor) const
Number of messages still waiting for the neighbor.
size_t sweep()
Drops every queued message whose time to live has run out; removes emptied queues.
NeighborState state(Key neighbor) const
Current state of a neighbor.
std::function< void(Key, const Frame &)> Deliver
std::chrono::steady_clock Clock
std::map< Key, std::deque< Entry > > _queues
std::vector< QueueHealth > queueHealth() const
Describes every non-empty queue (neighbor, depth, age of the oldest message).
OutgoingRouter(Deliver deliver, Now now=[] { return Clock::now();})
Creates a router with no neighbors known.
bool isReady(Key neighbor) const
True when the neighbor's state is NeighborState::ready.
bool send(Key neighbor, Frame frame, uint32_t ttlMs)
Delivers now if the neighbor is ready, otherwise queues the frame. Never waits. Expired queued messag...
std::map< Key, NeighborState > _states
bool markNotReady(Key neighbor)
The neighbor can no longer be reached for now. Messages sent to it wait in its queue again....
size_t discardQueue(Key neighbor)
For a neighbor declared down or unreachable: what was waiting for it may no longer make sense,...
NeighborState
Readiness of a neighbor process as seen by the router. unknown: nothing known yet; probing: readiness...
Clock::time_point queuedAt
std::optional< Clock::time_point > expires