Fray Engine
Loading...
Searching...
No Matches
outgoing_router.hpp
Go to the documentation of this file.
1/* outgoing_router.hpp */
2
3#ifndef _FRAY_OUTGOING_ROUTER_HPP_
4#define _FRAY_OUTGOING_ROUTER_HPP_
5
6#include <chrono>
7#include <cstddef>
8#include <cstdint>
9#include <deque>
10#include <functional>
11#include <map>
12#include <optional>
13#include <utility>
14#include <vector>
15
16namespace fray
17{
18 namespace routing
19 {
36 template <class Key, class Frame>
38 {
39 public:
41 using Clock = std::chrono::steady_clock;
43 using Deliver = std::function<void(Key, const Frame &)>;
45 using Now = std::function<Clock::time_point()>;
48 {
52 size_t depth;
54 uint64_t oldestAgeMs;
55 };
56
63 Deliver deliver,
64 Now now = [] { return Clock::now(); })
65 : _deliver(std::move(deliver)), _now(std::move(now)) {}
66
73 Key neighbor) const
74 {
75 const auto found = _states.find(neighbor);
76 return found == _states.end() ? NeighborState::unknown : found->second;
77 }
78
80 bool isReady(
81 Key neighbor) const { return state(neighbor) == NeighborState::ready; }
82
90 Key neighbor,
91 NeighborState value)
92 {
93 if (value == NeighborState::ready) return markReady(neighbor);
94 if (state(neighbor) == value) return false;
95 _states[neighbor] = value;
96 return true;
97 }
98
104 size_t queued(
105 Key neighbor) const
106 {
107 const auto found = _queues.find(neighbor);
108 return found == _queues.end() ? 0 : found->second.size();
109 }
110
115 std::vector<QueueHealth> queueHealth() const
116 {
117 std::vector<QueueHealth> result;
118 const auto now = _now();
119 for (const auto &[neighbor, queue] : _queues)
120 {
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())});
124 }
125 return result;
126 }
127
135 Key neighbor)
136 {
137 if (isReady(neighbor))
138 return false;
139 _states[neighbor] = NeighborState::ready;
140 const auto found = _queues.find(neighbor);
141 if (found == _queues.end())
142 return true;
143 auto waiting = std::move(found->second);
144 _queues.erase(found);
145 const auto now = _now();
146 for (auto &entry : waiting)
147 if (!entry.expires || *entry.expires > now)
148 _deliver(neighbor, entry.frame);
149 return true;
150 }
151
159 Key neighbor)
160 {
161 const bool wasReady = isReady(neighbor);
162 _states.erase(neighbor);
163 return wasReady;
164 }
165
172 Key neighbor)
173 {
174 const auto found = _queues.find(neighbor);
175 if (found == _queues.end())
176 return 0;
177 const size_t discarded = found->second.size();
178 _queues.erase(found);
179 return discarded;
180 }
181
190 bool send(
191 Key neighbor,
192 Frame frame,
193 uint32_t ttlMs)
194 {
195 if (isReady(neighbor))
196 {
197 _deliver(neighbor, frame);
198 return true;
199 }
200 sweep();
201 const auto now = _now();
202 Entry entry{std::move(frame), now, std::nullopt};
203 if (ttlMs > 0)
204 entry.expires = now + std::chrono::milliseconds(ttlMs);
205 _queues[neighbor].push_back(std::move(entry));
206 return false;
207 }
208
213 size_t sweep()
214 {
215 const auto now = _now();
216 size_t dropped = 0;
217 for (auto queue = _queues.begin(); queue != _queues.end();)
218 {
219 auto &entries = queue->second;
220 for (auto entry = entries.begin(); entry != entries.end();)
221 {
222 if (entry->expires && *entry->expires <= now)
223 {
224 entry = entries.erase(entry);
225 ++dropped;
226 }
227 else
228 ++entry;
229 }
230 queue = entries.empty() ? _queues.erase(queue) : std::next(queue);
231 }
232 return dropped;
233 }
234
235 private:
237 struct Entry
238 {
240 Frame frame;
242 Clock::time_point queuedAt;
244 std::optional<Clock::time_point> expires;
245 };
246
252 std::map<Key, NeighborState> _states;
254 std::map<Key, std::deque<Entry>> _queues;
255 };
256 }
257}
258
259#endif
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...
std::optional< Clock::time_point > expires