xrpld
Loading...
Searching...
No Matches
Counts.h
1#pragma once
2
3#include <xrpld/peerfinder/PeerfinderManager.h>
4#include <xrpld/peerfinder/Slot.h>
5#include <xrpld/peerfinder/detail/Tuning.h>
6
7#include <xrpl/basics/random.h>
8
9namespace xrpl::PeerFinder {
10
12enum class CountAdjustment : int { Decrement = -1, Increment = 1 };
13
15class Counts
16{
17public:
19 void
20 add(Slot const& s)
21 {
23 }
24
26 void
27 remove(Slot const& s)
28 {
30 }
31
33 [[nodiscard]] bool
34 canActivate(Slot const& s) const
35 {
36 // Must be handshaked and in the right state
37 XRPL_ASSERT(
39 "xrpl::PeerFinder::Counts::can_activate : valid input state");
40
41 if (s.fixed() || s.reserved())
42 return true;
43
44 if (s.inbound())
45 return inActive_ < inMax_;
46
47 return outActive_ < outMax_;
48 }
49
51 [[nodiscard]] std::size_t
53 {
55 return 0;
57 }
58
60 [[nodiscard]] std::size_t
61 attempts() const
62 {
63 return attempts_;
64 }
65
67 [[nodiscard]] int
68 outMax() const
69 {
70 return outMax_;
71 }
72
76 [[nodiscard]] int
77 outActive() const
78 {
79 return outActive_;
80 }
81
83 [[nodiscard]] std::size_t
84 fixed() const
85 {
86 return fixed_;
87 }
88
90 [[nodiscard]] std::size_t
92 {
93 return fixedActive_;
94 }
95
96 //--------------------------------------------------------------------------
97
99 void
100 onConfig(Config const& config)
101 {
102 outMax_ = config.outPeers;
103 if (config.wantIncoming)
104 inMax_ = config.inPeers;
105 }
106
108 [[nodiscard]] int
110 {
111 return acceptCount_;
112 }
113
115 [[nodiscard]] int
117 {
118 return attempts_;
119 }
120
122 [[nodiscard]] int
124 {
125 return closingCount_;
126 }
127
129 [[nodiscard]] int
130 inMax() const
131 {
132 return inMax_;
133 }
134
136 [[nodiscard]] int
138 {
139 return inActive_;
140 }
141
143 [[nodiscard]] int
145 {
146 return inActive_ + outActive_;
147 }
148
152 [[nodiscard]] int
154 {
155 if (inActive_ < inMax_)
156 return inMax_ - inActive_;
157 return 0;
158 }
159
163 [[nodiscard]] int
165 {
166 if (outActive_ < outMax_)
167 return outMax_ - outActive_;
168 return 0;
169 }
170
171 //--------------------------------------------------------------------------
172
175 [[nodiscard]] bool
177 {
178 // We will consider ourselves connected if we have reached
179 // the number of outgoing connections desired, or if connect
180 // automatically is false.
181 //
182 // Fixed peers do not count towards the active outgoing total.
183
184 return outMax_ <= 0;
185 }
186
188 void
190 {
191 map["accept"] = acceptCount();
192 map["connect"] = connectCount();
193 map["close"] = closingCount();
194 map["in"] << inActive_ << "/" << inMax_;
195 map["out"] << outActive_ << "/" << outMax_;
196 map["fixed"] = fixedActive_;
197 map["reserved"] = reserved_;
198 map["total"] = active_;
199 }
200
202 [[nodiscard]] std::string
204 {
206 ss << outActive_ << "/" << outMax_ << " out, " << inActive_ << "/" << inMax_ << " in, "
207 << connectCount() << " connecting, " << closingCount() << " closing";
208 return ss.str();
209 }
210
211 //--------------------------------------------------------------------------
212private:
214 template <typename T>
215 static void
217 {
218 switch (dir)
219 {
221 ++counter;
222 break;
224 --counter;
225 break;
226 }
227 }
228
229 // Adjusts counts based on the specified slot, in the direction indicated.
230 //
231 // IMPORTANT: All std::size_t counters MUST be adjusted via adjustCounter()
232 // and NEVER via `+= n` where n = static_cast<int>(dir). When dir is
233 // Decrement, n == -1; adding -1 to a std::size_t implicitly converts -1 to
234 // SIZE_MAX, which UBSan flags as unsigned-integer-overflow and masks real
235 // underflow bugs (decrementing a counter already at zero). Plain int
236 // counters (acceptCount_, attempts_, closingCount_) are safe with += n.
237 void
238 adjust(Slot const& s, CountAdjustment const dir)
239 {
240 int const n = static_cast<int>(dir);
241 if (s.fixed())
242 adjustCounter(fixed_, dir);
243
244 if (s.reserved())
246
247 switch (s.state())
248 {
250 XRPL_ASSERT(s.inbound(), "xrpl::PeerFinder::Counts::adjust : input is inbound");
251 acceptCount_ += n;
252 break;
253
256 XRPL_ASSERT(
257 !s.inbound(),
258 "xrpl::PeerFinder::Counts::adjust : input is not "
259 "inbound");
260 attempts_ += n;
261 break;
262
264 if (s.fixed())
266 if (!s.fixed() && !s.reserved())
267 {
268 if (s.inbound())
269 {
271 }
272 else
273 {
275 }
276 }
278 break;
279
281 closingCount_ += n;
282 break;
283
284 // LCOV_EXCL_START
285 default:
286 UNREACHABLE("xrpl::PeerFinder::Counts::adjust : invalid input state");
287 break;
288 // LCOV_EXCL_STOP
289 };
290 }
291
292private:
294 int attempts_{0};
295
298
301
304
307
310
313
316
319
320 // Number of inbound connections that are
321 // not active or gracefully closing.
323
324 // Number of connections that are gracefully closing.
326};
327
328} // namespace xrpl::PeerFinder
Manages the count of available connections for the various slots.
Definition Counts.h:16
void adjust(Slot const &s, CountAdjustment const dir)
Definition Counts.h:238
std::size_t inActive_
Number of inbound slots assigned to active peers.
Definition Counts.h:303
int inboundSlotsFree() const
Returns the number of unused inbound slots.
Definition Counts.h:153
std::size_t attemptsNeeded() const
Returns the number of attempts needed to bring us to the max.
Definition Counts.h:52
int outboundSlotsFree() const
Returns the number of unused outbound slots.
Definition Counts.h:164
int connectCount() const
Returns the number of connection attempts currently active.
Definition Counts.h:116
std::size_t active_
Active connections, including fixed and reserved.
Definition Counts.h:297
void remove(Slot const &s)
Removes the slot state and properties from the slot counts.
Definition Counts.h:27
int outActive() const
Returns the number of outbound peers assigned an open slot.
Definition Counts.h:77
std::size_t attempts() const
Returns the number of outbound connection attempts.
Definition Counts.h:61
int closingCount() const
Returns the number of connections that are gracefully closing.
Definition Counts.h:123
int outMax() const
Returns the total number of outbound slots.
Definition Counts.h:68
std::size_t inMax_
Total number of inbound slots.
Definition Counts.h:300
std::string stateString() const
Records the state for diagnostics.
Definition Counts.h:203
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition Counts.h:137
std::size_t outActive_
Active outbound slots.
Definition Counts.h:309
int inMax() const
Returns the total number of inbound slots.
Definition Counts.h:130
std::size_t reserved_
Reserved connections.
Definition Counts.h:318
static void adjustCounter(T &counter, CountAdjustment dir)
Increments or decrements a counter based on the adjustment direction.
Definition Counts.h:216
int acceptCount() const
Returns the number of accepted connections that haven't handshaked.
Definition Counts.h:109
std::size_t fixedActive_
Active fixed connections.
Definition Counts.h:315
std::size_t fixed_
Fixed connections.
Definition Counts.h:312
int totalActive() const
Returns the total number of active peers excluding fixed peers.
Definition Counts.h:144
int attempts_
Outbound connection attempts.
Definition Counts.h:294
std::size_t fixed() const
Returns the number of fixed connections.
Definition Counts.h:84
bool isConnectedToNetwork() const
Returns true if the slot logic considers us "connected" to the network.
Definition Counts.h:176
void onWrite(beast::PropertyStream::Map &map) const
Output statistics.
Definition Counts.h:189
void add(Slot const &s)
Adds the slot state and properties to the slot counts.
Definition Counts.h:20
void onConfig(Config const &config)
Called when the config is set or changed.
Definition Counts.h:100
std::size_t fixedActive() const
Returns the number of active fixed connections.
Definition Counts.h:91
bool canActivate(Slot const &s) const
Returns true if the slot can become active.
Definition Counts.h:34
std::size_t outMax_
Maximum desired outbound slots.
Definition Counts.h:306
Properties and state associated with a peer to peer overlay connection.
virtual bool reserved() const =0
Returns true if this is a reserved connection.
virtual State state() const =0
Returns the state of the connection.
virtual bool inbound() const =0
Returns true if this is an inbound connection.
virtual bool fixed() const =0
Returns true if this is a fixed connection.
static constexpr auto kMaxConnectAttempts
Maximum number of simultaneous connection attempts.
CountAdjustment
Direction of a slot count adjustment.
Definition Counts.h:12
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.