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 void
18 add(Slot const& s)
19 {
20 adjust(s, 1);
21 }
22
24 void
25 remove(Slot const& s)
26 {
27 adjust(s, -1);
28 }
29
31 bool
32 can_activate(Slot const& s) const
33 {
34 // Must be handshaked and in the right state
35 XRPL_ASSERT(
36 s.state() == Slot::connected || s.state() == Slot::accept,
37 "xrpl::PeerFinder::Counts::can_activate : valid input state");
38
39 if (s.fixed() || s.reserved())
40 return true;
41
42 if (s.inbound())
43 return m_in_active < m_in_max;
44
45 return m_out_active < m_out_max;
46 }
47
51 {
53 return 0;
55 }
56
59 attempts() const
60 {
61 return m_attempts;
62 }
63
65 int
66 out_max() const
67 {
68 return m_out_max;
69 }
70
74 int
75 out_active() const
76 {
77 return m_out_active;
78 }
79
82 fixed() const
83 {
84 return m_fixed;
85 }
86
90 {
91 return m_fixed_active;
92 }
93
94 //--------------------------------------------------------------------------
95
97 void
98 onConfig(Config const& config)
99 {
100 m_out_max = config.outPeers;
101 if (config.wantIncoming)
102 m_in_max = config.inPeers;
103 }
104
106 int
108 {
109 return m_acceptCount;
110 }
111
113 int
115 {
116 return m_attempts;
117 }
118
120 int
122 {
123 return m_closingCount;
124 }
125
127 int
128 in_max() const
129 {
130 return m_in_max;
131 }
132
134 int
136 {
137 return m_in_active;
138 }
139
141 int
143 {
144 return m_in_active + m_out_active;
145 }
146
150 int
152 {
153 if (m_in_active < m_in_max)
154 return m_in_max - m_in_active;
155 return 0;
156 }
157
161 int
163 {
165 return m_out_max - m_out_active;
166 return 0;
167 }
168
169 //--------------------------------------------------------------------------
170
173 bool
175 {
176 // We will consider ourselves connected if we have reached
177 // the number of outgoing connections desired, or if connect
178 // automatically is false.
179 //
180 // Fixed peers do not count towards the active outgoing total.
181
182 if (m_out_max > 0)
183 return false;
184
185 return true;
186 }
187
189 void
191 {
192 map["accept"] = acceptCount();
193 map["connect"] = connectCount();
194 map["close"] = closingCount();
195 map["in"] << m_in_active << "/" << m_in_max;
196 map["out"] << m_out_active << "/" << m_out_max;
197 map["fixed"] = m_fixed_active;
198 map["reserved"] = m_reserved;
199 map["total"] = m_active;
200 }
201
205 {
207 ss << m_out_active << "/" << m_out_max << " out, " << m_in_active << "/" << m_in_max
208 << " in, " << connectCount() << " connecting, " << closingCount() << " closing";
209 return ss.str();
210 }
211
212 //--------------------------------------------------------------------------
213private:
214 // Adjusts counts based on the specified slot, in the direction indicated.
215 void
216 adjust(Slot const& s, int const n)
217 {
218 if (s.fixed())
219 m_fixed += n;
220
221 if (s.reserved())
222 m_reserved += n;
223
224 switch (s.state())
225 {
226 case Slot::accept:
227 XRPL_ASSERT(s.inbound(), "xrpl::PeerFinder::Counts::adjust : input is inbound");
228 m_acceptCount += n;
229 break;
230
231 case Slot::connect:
232 case Slot::connected:
233 XRPL_ASSERT(
234 !s.inbound(),
235 "xrpl::PeerFinder::Counts::adjust : input is not "
236 "inbound");
237 m_attempts += n;
238 break;
239
240 case Slot::active:
241 if (s.fixed())
242 m_fixed_active += n;
243 if (!s.fixed() && !s.reserved())
244 {
245 if (s.inbound())
246 m_in_active += n;
247 else
248 m_out_active += n;
249 }
250 m_active += n;
251 break;
252
253 case Slot::closing:
254 m_closingCount += n;
255 break;
256
257 // LCOV_EXCL_START
258 default:
259 UNREACHABLE("xrpl::PeerFinder::Counts::adjust : invalid input state");
260 break;
261 // LCOV_EXCL_STOP
262 };
263 }
264
265private:
268
271
274
277
280
283
286
289
292
293 // Number of inbound connections that are
294 // not active or gracefully closing.
296
297 // Number of connections that are gracefully closing.
299};
300
301} // namespace PeerFinder
302} // 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:151
void onWrite(beast::PropertyStream::Map &map)
Output statistics.
Definition Counts.h:190
int outboundSlotsFree() const
Returns the number of unused outbound slots.
Definition Counts.h:162
std::size_t m_in_active
Number of inbound slots assigned to active peers.
Definition Counts.h:276
int connectCount() const
Returns the number of connection attempts currently active.
Definition Counts.h:114
void remove(Slot const &s)
Removes the slot state and properties from the slot counts.
Definition Counts.h:25
std::size_t attempts() const
Returns the number of outbound connection attempts.
Definition Counts.h:59
int closingCount() const
Returns the number of connections that are gracefully closing.
Definition Counts.h:121
std::size_t m_active
Active connections, including fixed and reserved.
Definition Counts.h:270
int m_attempts
Outbound connection attempts.
Definition Counts.h:267
int out_max() const
Returns the total number of outbound slots.
Definition Counts.h:66
int inboundActive() const
Returns the number of inbound peers assigned an open slot.
Definition Counts.h:135
std::size_t m_fixed
Fixed connections.
Definition Counts.h:285
bool can_activate(Slot const &s) const
Returns true if the slot can become active.
Definition Counts.h:32
void adjust(Slot const &s, int const n)
Definition Counts.h:216
int acceptCount() const
Returns the number of accepted connections that haven't handshaked.
Definition Counts.h:107
std::string state_string() const
Records the state for diagnostics.
Definition Counts.h:204
std::size_t m_fixed_active
Active fixed connections.
Definition Counts.h:288
int totalActive() const
Returns the total number of active peers excluding fixed peers.
Definition Counts.h:142
int in_max() const
Returns the total number of inbound slots.
Definition Counts.h:128
std::size_t fixed_active() const
Returns the number of active fixed connections.
Definition Counts.h:89
std::size_t fixed() const
Returns the number of fixed connections.
Definition Counts.h:82
bool isConnectedToNetwork() const
Returns true if the slot logic considers us "connected" to the network.
Definition Counts.h:174
void add(Slot const &s)
Adds the slot state and properties to the slot counts.
Definition Counts.h:18
void onConfig(Config const &config)
Called when the config is set or changed.
Definition Counts.h:98
std::size_t m_out_active
Active outbound slots.
Definition Counts.h:282
int out_active() const
Returns the number of outbound peers assigned an open slot.
Definition Counts.h:75
std::size_t m_reserved
Reserved connections.
Definition Counts.h:291
std::size_t m_out_max
Maximum desired outbound slots.
Definition Counts.h:279
std::size_t attempts_needed() const
Returns the number of attempts needed to bring us to the max.
Definition Counts.h:50
std::size_t m_in_max
Total number of inbound slots.
Definition Counts.h:273
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.