xrpld
Toggle main menu visibility
Loading...
Searching...
No Matches
libxrpl
resource
ResourceManager.cpp
1
#include <xrpl/resource/ResourceManager.h>
2
3
#include <xrpl/basics/chrono.h>
4
#include <xrpl/beast/core/CurrentThreadName.h>
5
#include <xrpl/beast/insight/Collector.h>
6
#include <xrpl/beast/net/IPAddressConversion.h>
7
#include <xrpl/beast/net/IPEndpoint.h>
8
#include <xrpl/beast/utility/Journal.h>
9
#include <xrpl/beast/utility/PropertyStream.h>
10
#include <xrpl/json/json_value.h>
11
#include <xrpl/resource/Consumer.h>
12
#include <xrpl/resource/Gossip.h>
13
#include <xrpl/resource/detail/Logic.h>
14
15
#include <boost/asio/ip/address.hpp>
16
#include <boost/system/detail/error_code.hpp>
17
18
#include <
chrono
>
19
#include <
condition_variable
>
20
#include <
memory
>
21
#include <
mutex
>
22
#include <
string
>
23
#include <
string_view
>
24
#include <
thread
>
25
26
namespace
xrpl::resource
{
27
28
class
ManagerImp
:
public
Manager
29
{
30
private
:
31
beast::Journal
const
journal_
;
32
Logic
logic_
;
33
std::thread
thread_
;
34
bool
stop_
=
false
;
35
std::mutex
mutex_
;
36
std::condition_variable
cond_
;
37
38
public
:
39
ManagerImp
(
beast::insight::Collector::Ptr
const
& collector,
beast::Journal
journal)
40
:
journal_
(journal),
logic_
(collector,
stopwatch
(), journal)
41
{
42
thread_
=
std::thread
{&
ManagerImp::run
,
this
};
43
}
44
45
ManagerImp
() =
delete
;
46
ManagerImp
(
ManagerImp
const
&) =
delete
;
47
ManagerImp
&
48
operator=
(
ManagerImp
const
&) =
delete
;
49
50
~ManagerImp
()
override
51
{
52
{
53
std::scoped_lock
const
lock(
mutex_
);
54
stop_
=
true
;
55
cond_
.notify_one();
56
}
57
thread_
.join();
58
}
59
60
Consumer
61
newInboundEndpoint
(
beast::ip::Endpoint
const
& address)
override
62
{
63
return
logic_
.newInboundEndpoint(address);
64
}
65
66
Consumer
67
newInboundEndpoint
(
68
beast::ip::Endpoint
const
& address,
69
bool
const
proxy,
70
std::string_view
forwardedFor
)
override
71
{
72
if
(!proxy)
73
return
newInboundEndpoint
(address);
74
75
boost::system::error_code ec;
76
auto
const
proxiedIp = boost::asio::ip::make_address(
forwardedFor
, ec);
77
if
(ec)
78
{
79
journal_
.warn() <<
"forwarded for ("
<<
forwardedFor
<<
") from proxy "
80
<< address.
toString
()
81
<<
" doesn't convert to IP endpoint: "
<< ec.message();
82
return
newInboundEndpoint
(address);
83
}
84
return
newInboundEndpoint
(
beast::IPAddressConversion::fromAsio
(proxiedIp));
85
}
86
87
Consumer
88
newOutboundEndpoint
(
beast::ip::Endpoint
const
& address)
override
89
{
90
return
logic_
.newOutboundEndpoint(address);
91
}
92
93
Consumer
94
newUnlimitedEndpoint
(
beast::ip::Endpoint
const
& address)
override
95
{
96
return
logic_
.newUnlimitedEndpoint(address);
97
}
98
99
Gossip
100
exportConsumers
()
override
101
{
102
return
logic_
.exportConsumers();
103
}
104
105
void
106
importConsumers
(
std::string
const
& origin,
Gossip
const
& gossip)
override
107
{
108
logic_
.importConsumers(origin, gossip);
109
}
110
111
//--------------------------------------------------------------------------
112
113
json::Value
114
getJson
()
override
115
{
116
return
logic_
.getJson();
117
}
118
119
json::Value
120
getJson
(
int
threshold)
override
121
{
122
return
logic_
.getJson(threshold);
123
}
124
125
//--------------------------------------------------------------------------
126
127
void
128
onWrite
(
beast::PropertyStream::Map
& map)
override
129
{
130
logic_
.onWrite(map);
131
}
132
133
//--------------------------------------------------------------------------
134
135
private
:
136
void
137
run
()
138
{
139
beast::setCurrentThreadName
(
"resource::Mngr"
);
140
for
(;;)
141
{
142
logic_
.periodicActivity();
143
std::unique_lock<std::mutex>
lock(
mutex_
);
144
cond_
.wait_for(lock,
std::chrono::seconds
(1));
145
if
(
stop_
)
146
break
;
147
}
148
}
149
};
150
151
//------------------------------------------------------------------------------
152
153
Manager::Manager
() :
beast
::PropertyStream::
Source
(
"resource"
)
154
{
155
}
156
157
Manager::~Manager
() =
default
;
158
159
//------------------------------------------------------------------------------
160
161
std::unique_ptr<Manager>
162
makeManager
(
beast::insight::Collector::Ptr
const
& collector,
beast::Journal
journal)
163
{
164
return
std::make_unique<ManagerImp>
(collector, journal);
165
}
166
167
}
// namespace xrpl::resource
std::string
std::string_view
chrono
beast::Journal
A generic endpoint for log messages.
Definition
Journal.h:44
beast::PropertyStream::Map
Definition
PropertyStream.h:207
beast::PropertyStream::Source::Source
Source(std::string name)
Definition
beast_PropertyStream.cpp:155
beast::insight::Collector::Ptr
std::shared_ptr< Collector > Ptr
Definition
Collector.h:29
beast::ip::Endpoint
A version-independent IP address and port combination.
Definition
IPEndpoint.h:24
beast::ip::Endpoint::toString
std::string toString() const
Returns a string representing the endpoint.
Definition
IPEndpoint.cpp:50
json::Value
Represents a JSON value.
Definition
json_value.h:117
xrpl::resource::Consumer
An endpoint that consumes resources.
Definition
Consumer.h:20
xrpl::resource::Logic
Definition
resource/detail/Logic.h:30
xrpl::resource::ManagerImp::run
void run()
Definition
ResourceManager.cpp:137
xrpl::resource::ManagerImp::logic_
Logic logic_
Definition
ResourceManager.cpp:32
xrpl::resource::ManagerImp::cond_
std::condition_variable cond_
Definition
ResourceManager.cpp:36
xrpl::resource::ManagerImp::ManagerImp
ManagerImp(beast::insight::Collector::Ptr const &collector, beast::Journal journal)
Definition
ResourceManager.cpp:39
xrpl::resource::ManagerImp::newUnlimitedEndpoint
Consumer newUnlimitedEndpoint(beast::ip::Endpoint const &address) override
Create a new unlimited endpoint keyed by forwarded IP.
Definition
ResourceManager.cpp:94
xrpl::resource::ManagerImp::thread_
std::thread thread_
Definition
ResourceManager.cpp:33
xrpl::resource::ManagerImp::onWrite
void onWrite(beast::PropertyStream::Map &map) override
Subclass override.
Definition
ResourceManager.cpp:128
xrpl::resource::ManagerImp::stop_
bool stop_
Definition
ResourceManager.cpp:34
xrpl::resource::ManagerImp::~ManagerImp
~ManagerImp() override
Definition
ResourceManager.cpp:50
xrpl::resource::ManagerImp::journal_
beast::Journal const journal_
Definition
ResourceManager.cpp:31
xrpl::resource::ManagerImp::exportConsumers
Gossip exportConsumers() override
Extract packaged consumer information for export.
Definition
ResourceManager.cpp:100
xrpl::resource::ManagerImp::operator=
ManagerImp & operator=(ManagerImp const &)=delete
xrpl::resource::ManagerImp::newOutboundEndpoint
Consumer newOutboundEndpoint(beast::ip::Endpoint const &address) override
Create a new endpoint keyed by outbound IP address and port.
Definition
ResourceManager.cpp:88
xrpl::resource::ManagerImp::getJson
json::Value getJson(int threshold) override
Definition
ResourceManager.cpp:120
xrpl::resource::ManagerImp::importConsumers
void importConsumers(std::string const &origin, Gossip const &gossip) override
Import packaged consumer information.
Definition
ResourceManager.cpp:106
xrpl::resource::ManagerImp::mutex_
std::mutex mutex_
Definition
ResourceManager.cpp:35
xrpl::resource::ManagerImp::newInboundEndpoint
Consumer newInboundEndpoint(beast::ip::Endpoint const &address, bool const proxy, std::string_view forwardedFor) override
Definition
ResourceManager.cpp:67
xrpl::resource::ManagerImp::ManagerImp
ManagerImp()=delete
xrpl::resource::ManagerImp::ManagerImp
ManagerImp(ManagerImp const &)=delete
xrpl::resource::ManagerImp::newInboundEndpoint
Consumer newInboundEndpoint(beast::ip::Endpoint const &address) override
Create a new endpoint keyed by inbound IP address or the forwarded IP if proxied.
Definition
ResourceManager.cpp:61
xrpl::resource::ManagerImp::getJson
json::Value getJson() override
Extract consumer information for reporting.
Definition
ResourceManager.cpp:114
xrpl::resource::Manager::Manager
Manager()
Definition
ResourceManager.cpp:153
xrpl::resource::Manager::~Manager
~Manager() override=0
condition_variable
std::chrono::seconds
std::make_unique
T make_unique(T... args)
memory
mutex
beast
Definition
base_uint.h:697
beast::setCurrentThreadName
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
Definition
CurrentThreadName.cpp:115
xrpl::resource
Definition
ServiceRegistry.h:21
xrpl::resource::makeManager
std::unique_ptr< Manager > makeManager(beast::insight::Collector::Ptr const &collector, beast::Journal journal)
Definition
ResourceManager.cpp:162
xrpl::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition
chrono.h:101
xrpl::forwardedFor
std::string_view forwardedFor(HttpRequestType const &request)
Definition
Role.cpp:251
std::scoped_lock
string
string_view
beast::IPAddressConversion::fromAsio
static ip::Endpoint fromAsio(boost::asio::ip::address const &address)
Definition
IPAddressConversion.h:45
xrpl::resource::Gossip
Data format for exchanging consumption information across peers.
Definition
Gossip.h:13
thread
std::unique_lock
std::unique_ptr
Generated by
1.17.0