rippled
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 {
10namespace PeerFinder {
11
13class Counts
14{
15public:
17 : m_attempts(0)
18 , m_active(0)
19 , m_in_max(0)
20 , m_in_active(0)
21 , m_out_max(0)
22 , m_out_active(0)
23 , m_fixed(0)
25 , m_reserved(0)
26
27 , m_acceptCount(0)
29 {
30 }
31
32 //--------------------------------------------------------------------------
33
35 void
36 add(Slot const& s)
37 {
38 adjust(s, 1);
39 }
40
42 void
43 remove(Slot const& s)
44 {
45 adjust(s, -1);
46 }
47
49 bool
50 can_activate(Slot const& s) const
51 {
52 // Must be handshaked and in the right state
53 XRPL_ASSERT(
54 s.state() == Slot::connected || s.state() == Slot::accept,
55 "xrpl::PeerFinder::Counts::can_activate : valid input state");
56
57 if (s.fixed() || s.reserved())
58 return true;
59
60 if (s.inbound())
61 return m_in_active < m_in_max;
62
63 return m_out_active < m_out_max;
64 }
65
69 {
71 return 0;
73 }
74
77 attempts() const
78 {
79 return m_attempts;
80 }
81
83 int
84 out_max() const
85 {
86 return m_out_max;
87 }
88
92 int
93 out_active() const
94 {
95 return m_out_active;
96 }
97
100 fixed() const
101 {
102 return m_fixed;
103 }
104
108 {
109 return m_fixed_active;
110 }
111
112 //--------------------------------------------------------------------------
113
115 void
116 onConfig(Config const& config)
117 {
118 m_out_max = config.outPeers;
119 if (config.wantIncoming)
120 m_in_max = config.inPeers;
121 }
122
124 int
126 {
127 return m_acceptCount;
128 }
129
131 int
133 {
134 return m_attempts;
135 }
136
138 int
140 {
141 return m_closingCount;
142 }
143
145 int
146 in_max() const
147 {
148 return m_in_max;
149 }
150
152 int
154 {
155 return m_in_active;
156 }
157
159 int
161 {
162 return m_in_active + m_out_active;
163 }
164
168 int
170 {
171 if (m_in_active < m_in_max)
172 return m_in_max - m_in_active;
173 return 0;
174 }
175
179 int
181 {
183 return m_out_max - m_out_active;
184 return 0;
185 }
186
187 //--------------------------------------------------------------------------
188
191 bool
193 {
194 // We will consider ourselves connected if we have reached
195 // the number of outgoing connections desired, or if connect
196 // automatically is false.
197 //
198 // Fixed peers do not count towards the active outgoing total.
199
200 if (m_out_max > 0)
201 return false;
202
203 return true;
204 }
205
207 void
209 {
210 map["accept"] = acceptCount();
211 map["connect"] = connectCount();
212 map["close"] = closingCount();
213 map["in"] << m_in_active << "/" << m_in_max;
214 map["out"] << m_out_active << "/" << m_out_max;
215 map["fixed"] = m_fixed_active;
216 map["reserved"] = m_reserved;
217 map["total"] = m_active;
218 }
219
223 {
225 ss << m_out_active << "/" << m_out_max << " out, " << m_in_active << "/" << m_in_max << " in, "
226 << connectCount() << " connecting, " << closingCount() << " closing";
227 return ss.str();
228 }
229
230 //--------------------------------------------------------------------------
231private:
232 // Adjusts counts based on the specified slot, in the direction indicated.
233 void
234 adjust(Slot const& s, int const n)
235 {
236 if (s.fixed())
237 m_fixed += n;
238
239 if (s.reserved())
240 m_reserved += n;
241
242 switch (s.state())
243 {
244 case Slot::accept:
245 XRPL_ASSERT(s.inbound(), "xrpl::PeerFinder::Counts::adjust : input is inbound");
246 m_acceptCount += n;
247 break;
248
249 case Slot::connect:
250 case Slot::connected:
251 XRPL_ASSERT(
252 !s.inbound(),
253 "xrpl::PeerFinder::Counts::adjust : input is not "
254 "inbound");
255 m_attempts += n;
256 break;
257
258 case Slot::active:
259 if (s.fixed())
260 m_fixed_active += n;
261 if (!s.fixed() && !s.reserved())
262 {
263 if (s.inbound())
264 m_in_active += n;
265 else
266 m_out_active += n;
267 }
268 m_active += n;
269 break;
270
271 case Slot::closing:
272 m_closingCount += n;
273 break;
274
275 // LCOV_EXCL_START
276 default:
277 UNREACHABLE("xrpl::PeerFinder::Counts::adjust : invalid input state");
278 break;
279 // LCOV_EXCL_STOP
280 };
281 }
282
283private:
286
289
292
295
298
301
304
307
310
311 // Number of inbound connections that are
312 // not active or gracefully closing.
314
315 // Number of connections that are gracefully closing.
317};
318
319} // namespace PeerFinder
320} // namespace xrpl
Manages the count of available connections for the various slots.
Definition Counts.h:14
int inboundSlotsFree() const
Returns the number of unused inbound slots.
Definition Counts.h:169
void onWrite(beast::PropertyStream::Map &map)
Output statistics.
Definition Counts.h:208
int outboundSlotsFree() const
Returns the number of unused outbound slots.
Definition Counts.h:180
std::size_t m_in_active
Number of inbound slots assigned to active peers.
Definition Counts.h:294
int connectCount() const
Returns the number of connection attempts currently active.
Definition Counts.h:132
void remove(Slot const &s)
Removes the slot state and properties from the slot counts.
Definition Counts.h:43
std::size_t attempts() const
Returns the number of outbound connection attempts.
Definition Counts.h:77
int closingCount() const
Returns the number of connections that are gracefully closing.
Definition Counts.h:139
std::size_t m_active
Active connections, including fixed and reserved.
Definition Counts.h:288
int m_attempts
Outbound connection attempts.
Definition Counts.h:285
int out_max() const
Returns the total number of outbound slots.
Definition Counts.h:84
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition Counts.h:153
std::size_t m_fixed
Fixed connections.
Definition Counts.h:303
bool can_activate(Slot const &s) const
Returns true if the slot can become active.
Definition Counts.h:50
void adjust(Slot const &s, int const n)
Definition Counts.h:234
int acceptCount() const
Returns the number of accepted connections that haven't handshaked.
Definition Counts.h:125
std::string state_string() const
Records the state for diagnostics.
Definition Counts.h:222
std::size_t m_fixed_active
Active fixed connections.
Definition Counts.h:306
int totalActive() const
Returns the total number of active peers excluding fixed peers.
Definition Counts.h:160
int in_max() const
Returns the total number of inbound slots.
Definition Counts.h:146
std::size_t fixed_active() const
Returns the number of active fixed connections.
Definition Counts.h:107
std::size_t fixed() const
Returns the number of fixed connections.
Definition Counts.h:100
bool isConnectedToNetwork() const
Returns true if the slot logic considers us "connected" to the network.
Definition Counts.h:192
void add(Slot const &s)
Adds the slot state and properties to the slot counts.
Definition Counts.h:36
void onConfig(Config const &config)
Called when the config is set or changed.
Definition Counts.h:116
std::size_t m_out_active
Active outbound slots.
Definition Counts.h:300
int out_active() const
Returns the number of outbound peers assigned an open slot.
Definition Counts.h:93
std::size_t m_reserved
Reserved connections.
Definition Counts.h:309
std::size_t m_out_max
Maximum desired outbound slots.
Definition Counts.h:297
std::size_t attempts_needed() const
Returns the number of attempts needed to bring us to the max.
Definition Counts.h:68
std::size_t m_in_max
Total number of inbound slots.
Definition Counts.h:291
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.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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.