xrpld
Loading...
Searching...
No Matches
Counts.h
1#pragma once
2
3#include <xrpl/beast/utility/PropertyStream.h>
4#include <xrpl/beast/utility/instrumentation.h>
5#include <xrpl/peerfinder/Config.h>
6#include <xrpl/peerfinder/Slot.h>
7#include <xrpl/peerfinder/detail/Tuning.h>
8
9#include <cstddef>
10#include <sstream>
11#include <string>
12
13namespace xrpl::peer_finder {
14
18enum class CountAdjustment : int { Decrement = -1, Increment = 1 };
19
23class Counts
24{
25public:
29 void
30 add(Slot const& s)
31 {
33 }
34
38 void
39 remove(Slot const& s)
40 {
42 }
43
47 [[nodiscard]] bool
48 canActivate(Slot const& s) const
49 {
50 // Must be handshaked and in the right state
51 XRPL_ASSERT(
53 "xrpl::peer_finder::Counts::can_activate : valid input state");
54
55 if (s.fixed() || s.reserved())
56 return true;
57
58 if (s.inbound())
59 return inActive_ < inMax_;
60
61 return outActive_ < outMax_;
62 }
63
67 [[nodiscard]] std::size_t
69 {
71 return 0;
73 }
74
78 [[nodiscard]] std::size_t
79 attempts() const
80 {
81 return attempts_;
82 }
83
87 [[nodiscard]] int
88 outMax() const
89 {
90 return outMax_;
91 }
92
97 [[nodiscard]] int
98 outActive() const
99 {
100 return outActive_;
101 }
102
106 [[nodiscard]] std::size_t
107 fixed() const
108 {
109 return fixed_;
110 }
111
115 [[nodiscard]] std::size_t
117 {
118 return fixedActive_;
119 }
120
121 //--------------------------------------------------------------------------
122
126 void
127 onConfig(Config const& config)
128 {
129 outMax_ = config.outPeers;
130 if (config.wantIncoming)
131 inMax_ = config.inPeers;
132 }
133
137 [[nodiscard]] int
139 {
140 return acceptCount_;
141 }
142
146 [[nodiscard]] int
148 {
149 return attempts_;
150 }
151
155 [[nodiscard]] int
157 {
158 return closingCount_;
159 }
160
164 [[nodiscard]] int
165 inMax() const
166 {
167 return inMax_;
168 }
169
173 [[nodiscard]] int
175 {
176 return inActive_;
177 }
178
182 [[nodiscard]] int
184 {
185 return inActive_ + outActive_;
186 }
187
192 [[nodiscard]] int
194 {
195 if (inActive_ < inMax_)
196 return inMax_ - inActive_;
197 return 0;
198 }
199
204 [[nodiscard]] int
206 {
207 if (outActive_ < outMax_)
208 return outMax_ - outActive_;
209 return 0;
210 }
211
212 //--------------------------------------------------------------------------
213
217 [[nodiscard]] bool
219 {
220 // We will consider ourselves connected if we have reached
221 // the number of outgoing connections desired, or if connect
222 // automatically is false.
223 //
224 // Fixed peers do not count towards the active outgoing total.
225
226 return outMax_ <= 0;
227 }
228
232 void
234 {
235 map["accept"] = acceptCount();
236 map["connect"] = connectCount();
237 map["close"] = closingCount();
238 map["in"] << inActive_ << "/" << inMax_;
239 map["out"] << outActive_ << "/" << outMax_;
240 map["fixed"] = fixedActive_;
241 map["reserved"] = reserved_;
242 map["total"] = active_;
243 }
244
248 [[nodiscard]] std::string
250 {
252 ss << outActive_ << "/" << outMax_ << " out, " << inActive_ << "/" << inMax_ << " in, "
253 << connectCount() << " connecting, " << closingCount() << " closing";
254 return ss.str();
255 }
256
257 //--------------------------------------------------------------------------
258private:
262 template <typename T>
263 static void
265 {
266 switch (dir)
267 {
269 ++counter;
270 break;
272 --counter;
273 break;
274 }
275 }
276
277 // Adjusts counts based on the specified slot, in the direction indicated.
278 //
279 // IMPORTANT: All std::size_t counters MUST be adjusted via adjustCounter()
280 // and NEVER via `+= n` where n = static_cast<int>(dir). When dir is
281 // Decrement, n == -1; adding -1 to a std::size_t implicitly converts -1 to
282 // SIZE_MAX, which UBSan flags as unsigned-integer-overflow and masks real
283 // underflow bugs (decrementing a counter already at zero). Plain int
284 // counters (acceptCount_, attempts_, closingCount_) are safe with += n.
285 void
286 adjust(Slot const& s, CountAdjustment const dir)
287 {
288 int const n = static_cast<int>(dir);
289 if (s.fixed())
290 adjustCounter(fixed_, dir);
291
292 if (s.reserved())
294
295 switch (s.state())
296 {
298 XRPL_ASSERT(s.inbound(), "xrpl::peer_finder::Counts::adjust : input is inbound");
299 acceptCount_ += n;
300 break;
301
304 XRPL_ASSERT(
305 !s.inbound(),
306 "xrpl::peer_finder::Counts::adjust : input is not "
307 "inbound");
308 attempts_ += n;
309 break;
310
312 if (s.fixed())
314 if (!s.fixed() && !s.reserved())
315 {
316 if (s.inbound())
317 {
319 }
320 else
321 {
323 }
324 }
326 break;
327
329 closingCount_ += n;
330 break;
331
332 // LCOV_EXCL_START
333 default:
334 UNREACHABLE("xrpl::peer_finder::Counts::adjust : invalid input state");
335 break;
336 // LCOV_EXCL_STOP
337 };
338 }
339
340private:
344 int attempts_{0};
345
350
355
360
365
370
375
380
385
386 // Number of inbound connections that are
387 // not active or gracefully closing.
389
390 // Number of connections that are gracefully closing.
392};
393
394} // namespace xrpl::peer_finder
Manages the count of available connections for the various slots.
Definition Counts.h:24
void remove(Slot const &s)
Removes the slot state and properties from the slot counts.
Definition Counts.h:39
std::size_t fixed_
Fixed connections.
Definition Counts.h:374
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition Counts.h:174
void adjust(Slot const &s, CountAdjustment const dir)
Definition Counts.h:286
std::size_t outMax_
Maximum desired outbound slots.
Definition Counts.h:364
static void adjustCounter(T &counter, CountAdjustment dir)
Increments or decrements a counter based on the adjustment direction.
Definition Counts.h:264
int closingCount() const
Returns the number of connections that are gracefully closing.
Definition Counts.h:156
std::size_t fixedActive() const
Returns the number of active fixed connections.
Definition Counts.h:116
void onWrite(beast::PropertyStream::Map &map) const
Output statistics.
Definition Counts.h:233
int outMax() const
Returns the total number of outbound slots.
Definition Counts.h:88
bool canActivate(Slot const &s) const
Returns true if the slot can become active.
Definition Counts.h:48
std::size_t inActive_
Number of inbound slots assigned to active peers.
Definition Counts.h:359
bool isConnectedToNetwork() const
Returns true if the slot logic considers us "connected" to the network.
Definition Counts.h:218
std::size_t reserved_
Reserved connections.
Definition Counts.h:384
std::size_t outActive_
Active outbound slots.
Definition Counts.h:369
void onConfig(Config const &config)
Called when the config is set or changed.
Definition Counts.h:127
std::string stateString() const
Records the state for diagnostics.
Definition Counts.h:249
int acceptCount() const
Returns the number of accepted connections that haven't handshaked.
Definition Counts.h:138
int outActive() const
Returns the number of outbound peers assigned an open slot.
Definition Counts.h:98
int inMax() const
Returns the total number of inbound slots.
Definition Counts.h:165
int totalActive() const
Returns the total number of active peers excluding fixed peers.
Definition Counts.h:183
std::size_t inMax_
Total number of inbound slots.
Definition Counts.h:354
std::size_t attempts() const
Returns the number of outbound connection attempts.
Definition Counts.h:79
int inboundSlotsFree() const
Returns the number of unused inbound slots.
Definition Counts.h:193
void add(Slot const &s)
Adds the slot state and properties to the slot counts.
Definition Counts.h:30
int outboundSlotsFree() const
Returns the number of unused outbound slots.
Definition Counts.h:205
std::size_t fixedActive_
Active fixed connections.
Definition Counts.h:379
std::size_t attemptsNeeded() const
Returns the number of attempts needed to bring us to the max.
Definition Counts.h:68
int attempts_
Outbound connection attempts.
Definition Counts.h:344
std::size_t fixed() const
Returns the number of fixed connections.
Definition Counts.h:107
int connectCount() const
Returns the number of connection attempts currently active.
Definition Counts.h:147
std::size_t active_
Active connections, including fixed and reserved.
Definition Counts.h:349
Properties and state associated with a peer to peer overlay connection.
virtual State state() const =0
Returns the state of the connection.
virtual bool fixed() const =0
Returns true if this is a fixed connection.
virtual bool inbound() const =0
Returns true if this is an inbound connection.
virtual bool reserved() const =0
Returns true if this is a reserved connection.
static constexpr auto kMaxConnectAttempts
Maximum number of simultaneous connection attempts.
CountAdjustment
Direction of a slot count adjustment.
Definition Counts.h:18
T str(T... args)
PeerFinder configuration settings.
std::size_t outPeers
The number of automatic outbound connections to maintain.
bool wantIncoming
true if we want to accept incoming connections.
std::size_t inPeers
The number of automatic inbound connections to maintain.