rippled
Loading...
Searching...
No Matches
ResourceManager.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/basics/chrono.h>
21#include <xrpl/beast/core/CurrentThreadName.h>
22#include <xrpl/beast/insight/Collector.h>
23#include <xrpl/beast/net/IPAddressConversion.h>
24#include <xrpl/beast/net/IPEndpoint.h>
25#include <xrpl/beast/utility/Journal.h>
26#include <xrpl/beast/utility/PropertyStream.h>
27#include <xrpl/json/json_value.h>
28#include <xrpl/resource/Consumer.h>
29#include <xrpl/resource/Gossip.h>
30#include <xrpl/resource/ResourceManager.h>
31#include <xrpl/resource/detail/Logic.h>
32
33#include <boost/asio/ip/address.hpp>
34#include <boost/system/detail/error_code.hpp>
35
36#include <chrono>
37#include <condition_variable>
38#include <memory>
39#include <mutex>
40#include <string>
41#include <string_view>
42#include <thread>
43
44namespace ripple {
45namespace Resource {
46
47class ManagerImp : public Manager
48{
49private:
53 bool stop_ = false;
56
57public:
59 beast::insight::Collector::ptr const& collector,
60 beast::Journal journal)
61 : journal_(journal), logic_(collector, stopwatch(), journal)
62 {
64 }
65
66 ManagerImp() = delete;
67 ManagerImp(ManagerImp const&) = delete;
69 operator=(ManagerImp const&) = delete;
70
71 ~ManagerImp() override
72 {
73 {
75 stop_ = true;
77 }
78 thread_.join();
79 }
80
82 newInboundEndpoint(beast::IP::Endpoint const& address) override
83 {
84 return logic_.newInboundEndpoint(address);
85 }
86
89 beast::IP::Endpoint const& address,
90 bool const proxy,
92 {
93 if (!proxy)
94 return newInboundEndpoint(address);
95
96 boost::system::error_code ec;
97 auto const proxiedIp = boost::asio::ip::make_address(forwardedFor, ec);
98 if (ec)
99 {
100 journal_.warn()
101 << "forwarded for (" << forwardedFor << ") from proxy "
102 << address.to_string()
103 << " doesn't convert to IP endpoint: " << ec.message();
104 return newInboundEndpoint(address);
105 }
106 return newInboundEndpoint(
108 }
109
112 {
113 return logic_.newOutboundEndpoint(address);
114 }
115
118 {
119 return logic_.newUnlimitedEndpoint(address);
120 }
121
122 Gossip
124 {
125 return logic_.exportConsumers();
126 }
127
128 void
129 importConsumers(std::string const& origin, Gossip const& gossip) override
130 {
131 logic_.importConsumers(origin, gossip);
132 }
133
134 //--------------------------------------------------------------------------
135
137 getJson() override
138 {
139 return logic_.getJson();
140 }
141
143 getJson(int threshold) override
144 {
145 return logic_.getJson(threshold);
146 }
147
148 //--------------------------------------------------------------------------
149
150 void
152 {
153 logic_.onWrite(map);
154 }
155
156 //--------------------------------------------------------------------------
157
158private:
159 void
161 {
162 beast::setCurrentThreadName("Resource::Manager");
163 for (;;)
164 {
168 if (stop_)
169 break;
170 }
171 }
172};
173
174//------------------------------------------------------------------------------
175
176Manager::Manager() : beast::PropertyStream::Source("resource")
177{
178}
179
180Manager::~Manager() = default;
181
182//------------------------------------------------------------------------------
183
186 beast::insight::Collector::ptr const& collector,
187 beast::Journal journal)
188{
189 return std::make_unique<ManagerImp>(collector, journal);
190}
191
192} // namespace Resource
193} // namespace ripple
Represents a JSON value.
Definition json_value.h:149
A version-independent IP address and port combination.
Definition IPEndpoint.h:38
std::string to_string() const
Returns a string representing the endpoint.
A generic endpoint for log messages.
Definition Journal.h:60
Stream warn() const
Definition Journal.h:340
An endpoint that consumes resources.
Definition Consumer.h:35
Consumer newInboundEndpoint(beast::IP::Endpoint const &address)
void importConsumers(std::string const &origin, Gossip const &gossip)
Consumer newUnlimitedEndpoint(beast::IP::Endpoint const &address)
Create endpoint that should not have resource limits applied.
void onWrite(beast::PropertyStream::Map &map)
Consumer newOutboundEndpoint(beast::IP::Endpoint const &address)
std::condition_variable cond_
Json::Value getJson() override
Extract consumer information for reporting.
Consumer newOutboundEndpoint(beast::IP::Endpoint const &address) override
Create a new endpoint keyed by outbound IP address and port.
Consumer newUnlimitedEndpoint(beast::IP::Endpoint const &address) override
Create a new unlimited endpoint keyed by forwarded IP.
Gossip exportConsumers() override
Extract packaged consumer information for export.
ManagerImp(ManagerImp const &)=delete
void onWrite(beast::PropertyStream::Map &map) override
Subclass override.
Json::Value getJson(int threshold) override
Consumer newInboundEndpoint(beast::IP::Endpoint const &address, bool const proxy, std::string_view forwardedFor) override
Consumer newInboundEndpoint(beast::IP::Endpoint const &address) override
Create a new endpoint keyed by inbound IP address or the forwarded IP if proxied.
beast::Journal const journal_
ManagerImp & operator=(ManagerImp const &)=delete
void importConsumers(std::string const &origin, Gossip const &gossip) override
Import packaged consumer information.
ManagerImp(beast::insight::Collector::ptr const &collector, beast::Journal journal)
Tracks load and resource consumption.
T is_same_v
T join(T... args)
void setCurrentThreadName(std::string_view newThreadName)
Changes the name of the caller thread.
std::unique_ptr< Manager > make_Manager(beast::insight::Collector::ptr const &collector, beast::Journal journal)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
std::string_view forwardedFor(http_request_type const &request)
Definition Role.cpp:262
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition chrono.h:119
static IP::Endpoint from_asio(boost::asio::ip::address const &address)
Data format for exchanging consumption information across peers.
Definition Gossip.h:32