rippled
Loading...
Searching...
No Matches
Counts.h
1#ifndef XRPL_PEERFINDER_COUNTS_H_INCLUDED
2#define XRPL_PEERFINDER_COUNTS_H_INCLUDED
3
4#include <xrpld/peerfinder/PeerfinderManager.h>
5#include <xrpld/peerfinder/Slot.h>
6#include <xrpld/peerfinder/detail/Tuning.h>
7
8#include <xrpl/basics/random.h>
9
10namespace ripple {
11namespace PeerFinder {
12
14class Counts
15{
16public:
18 : m_attempts(0)
19 , m_active(0)
20 , m_in_max(0)
21 , m_in_active(0)
22 , m_out_max(0)
23 , m_out_active(0)
24 , m_fixed(0)
26 , m_reserved(0)
27
28 , m_acceptCount(0)
30 {
31 }
32
33 //--------------------------------------------------------------------------
34
36 void
37 add(Slot const& s)
38 {
39 adjust(s, 1);
40 }
41
43 void
44 remove(Slot const& s)
45 {
46 adjust(s, -1);
47 }
48
50 bool
51 can_activate(Slot const& s) const
52 {
53 // Must be handshaked and in the right state
54 XRPL_ASSERT(
55 s.state() == Slot::connected || s.state() == Slot::accept,
56 "ripple::PeerFinder::Counts::can_activate : valid input state");
57
58 if (s.fixed() || s.reserved())
59 return true;
60
61 if (s.inbound())
62 return m_in_active < m_in_max;
63
64 return m_out_active < m_out_max;
65 }
66
70 {
72 return 0;
74 }
75
78 attempts() const
79 {
80 return m_attempts;
81 }
82
84 int
85 out_max() const
86 {
87 return m_out_max;
88 }
89
93 int
94 out_active() const
95 {
96 return m_out_active;
97 }
98
101 fixed() const
102 {
103 return m_fixed;
104 }
105
109 {
110 return m_fixed_active;
111 }
112
113 //--------------------------------------------------------------------------
114
116 void
117 onConfig(Config const& config)
118 {
119 m_out_max = config.outPeers;
120 if (config.wantIncoming)
121 m_in_max = config.inPeers;
122 }
123
125 int
127 {
128 return m_acceptCount;
129 }
130
132 int
134 {
135 return m_attempts;
136 }
137
139 int
141 {
142 return m_closingCount;
143 }
144
146 int
147 in_max() const
148 {
149 return m_in_max;
150 }
151
153 int
155 {
156 return m_in_active;
157 }
158
160 int
162 {
163 return m_in_active + m_out_active;
164 }
165
169 int
171 {
172 if (m_in_active < m_in_max)
173 return m_in_max - m_in_active;
174 return 0;
175 }
176
180 int
182 {
184 return m_out_max - m_out_active;
185 return 0;
186 }
187
188 //--------------------------------------------------------------------------
189
192 bool
194 {
195 // We will consider ourselves connected if we have reached
196 // the number of outgoing connections desired, or if connect
197 // automatically is false.
198 //
199 // Fixed peers do not count towards the active outgoing total.
200
201 if (m_out_max > 0)
202 return false;
203
204 return true;
205 }
206
208 void
210 {
211 map["accept"] = acceptCount();
212 map["connect"] = connectCount();
213 map["close"] = closingCount();
214 map["in"] << m_in_active << "/" << m_in_max;
215 map["out"] << m_out_active << "/" << m_out_max;
216 map["fixed"] = m_fixed_active;
217 map["reserved"] = m_reserved;
218 map["total"] = m_active;
219 }
220
224 {
226 ss << m_out_active << "/" << m_out_max << " out, " << m_in_active << "/"
227 << m_in_max << " in, " << connectCount() << " connecting, "
228 << closingCount() << " closing";
229 return ss.str();
230 }
231
232 //--------------------------------------------------------------------------
233private:
234 // Adjusts counts based on the specified slot, in the direction indicated.
235 void
236 adjust(Slot const& s, int const n)
237 {
238 if (s.fixed())
239 m_fixed += n;
240
241 if (s.reserved())
242 m_reserved += n;
243
244 switch (s.state())
245 {
246 case Slot::accept:
247 XRPL_ASSERT(
248 s.inbound(),
249 "ripple::PeerFinder::Counts::adjust : input is inbound");
250 m_acceptCount += n;
251 break;
252
253 case Slot::connect:
254 case Slot::connected:
255 XRPL_ASSERT(
256 !s.inbound(),
257 "ripple::PeerFinder::Counts::adjust : input is not "
258 "inbound");
259 m_attempts += n;
260 break;
261
262 case Slot::active:
263 if (s.fixed())
264 m_fixed_active += n;
265 if (!s.fixed() && !s.reserved())
266 {
267 if (s.inbound())
268 m_in_active += n;
269 else
270 m_out_active += n;
271 }
272 m_active += n;
273 break;
274
275 case Slot::closing:
276 m_closingCount += n;
277 break;
278
279 // LCOV_EXCL_START
280 default:
281 UNREACHABLE(
282 "ripple::PeerFinder::Counts::adjust : invalid input state");
283 break;
284 // LCOV_EXCL_STOP
285 };
286 }
287
288private:
291
294
297
300
303
306
309
312
315
316 // Number of inbound connections that are
317 // not active or gracefully closing.
319
320 // Number of connections that are gracefully closing.
322};
323
324} // namespace PeerFinder
325} // namespace ripple
326
327#endif
Manages the count of available connections for the various slots.
Definition Counts.h:15
std::size_t m_out_active
Active outbound slots.
Definition Counts.h:305
std::size_t fixed_active() const
Returns the number of active fixed connections.
Definition Counts.h:108
std::string state_string() const
Records the state for diagnostics.
Definition Counts.h:223
int outboundSlotsFree() const
Returns the number of unused outbound slots.
Definition Counts.h:181
int closingCount() const
Returns the number of connections that are gracefully closing.
Definition Counts.h:140
int acceptCount() const
Returns the number of accepted connections that haven't handshaked.
Definition Counts.h:126
std::size_t fixed() const
Returns the number of fixed connections.
Definition Counts.h:101
bool isConnectedToNetwork() const
Returns true if the slot logic considers us "connected" to the network.
Definition Counts.h:193
int out_active() const
Returns the number of outbound peers assigned an open slot.
Definition Counts.h:94
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition Counts.h:154
std::size_t m_reserved
Reserved connections.
Definition Counts.h:314
std::size_t attempts_needed() const
Returns the number of attempts needed to bring us to the max.
Definition Counts.h:69
int m_attempts
Outbound connection attempts.
Definition Counts.h:290
void onWrite(beast::PropertyStream::Map &map)
Output statistics.
Definition Counts.h:209
std::size_t m_in_active
Number of inbound slots assigned to active peers.
Definition Counts.h:299
int out_max() const
Returns the total number of outbound slots.
Definition Counts.h:85
std::size_t m_fixed_active
Active fixed connections.
Definition Counts.h:311
void add(Slot const &s)
Adds the slot state and properties to the slot counts.
Definition Counts.h:37
int inboundSlotsFree() const
Returns the number of unused inbound slots.
Definition Counts.h:170
int in_max() const
Returns the total number of inbound slots.
Definition Counts.h:147
int connectCount() const
Returns the number of connection attempts currently active.
Definition Counts.h:133
int totalActive() const
Returns the total number of active peers excluding fixed peers.
Definition Counts.h:161
std::size_t m_fixed
Fixed connections.
Definition Counts.h:308
void adjust(Slot const &s, int const n)
Definition Counts.h:236
void remove(Slot const &s)
Removes the slot state and properties from the slot counts.
Definition Counts.h:44
std::size_t m_active
Active connections, including fixed and reserved.
Definition Counts.h:293
void onConfig(Config const &config)
Called when the config is set or changed.
Definition Counts.h:117
std::size_t m_in_max
Total number of inbound slots.
Definition Counts.h:296
std::size_t m_out_max
Maximum desired outbound slots.
Definition Counts.h:302
bool can_activate(Slot const &s) const
Returns true if the slot can become active.
Definition Counts.h:51
std::size_t attempts() const
Returns the number of outbound connection attempts.
Definition Counts.h:78
Properties and state associated with a peer to peer overlay 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.
virtual State state() const =0
Returns the state of the connection.
virtual bool reserved() const =0
Returns true if this is a reserved connection.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
T str(T... args)
PeerFinder configuration settings.
std::size_t inPeers
The number of automatic inbound connections to maintain.
bool wantIncoming
true if we want to accept incoming connections.
std::size_t outPeers
The number of automatic outbound connections to maintain.