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/Log.h>
21#include <xrpl/basics/chrono.h>
22#include <xrpl/beast/core/CurrentThreadName.h>
23#include <xrpl/beast/net/IPAddressConversion.h>
24#include <xrpl/resource/ResourceManager.h>
25#include <xrpl/resource/detail/Logic.h>
26#include <boost/asio/ip/address_v4.hpp>
27#include <boost/core/ignore_unused.hpp>
28#include <boost/system/error_code.hpp>
29#include <condition_variable>
30#include <memory>
31#include <mutex>
32
33namespace ripple {
34namespace Resource {
35
36class ManagerImp : public Manager
37{
38private:
42 bool stop_ = false;
45
46public:
48 beast::insight::Collector::ptr const& collector,
49 beast::Journal journal)
50 : journal_(journal), logic_(collector, stopwatch(), journal)
51 {
53 }
54
55 ManagerImp() = delete;
56 ManagerImp(ManagerImp const&) = delete;
58 operator=(ManagerImp const&) = delete;
59
60 ~ManagerImp() override
61 {
62 {
64 stop_ = true;
66 }
67 thread_.join();
68 }
69
71 newInboundEndpoint(beast::IP::Endpoint const& address) override
72 {
73 return logic_.newInboundEndpoint(address);
74 }
75
78 beast::IP::Endpoint const& address,
79 bool const proxy,
81 {
82 if (!proxy)
83 return newInboundEndpoint(address);
84
85 boost::system::error_code ec;
86 auto const proxiedIp = boost::asio::ip::make_address(forwardedFor, ec);
87 if (ec)
88 {
90 << "forwarded for (" << forwardedFor << ") from proxy "
91 << address.to_string()
92 << " doesn't convert to IP endpoint: " << ec.message();
93 return newInboundEndpoint(address);
94 }
95 return newInboundEndpoint(
97 }
98
101 {
102 return logic_.newOutboundEndpoint(address);
103 }
104
107 {
108 return logic_.newUnlimitedEndpoint(address);
109 }
110
111 Gossip
113 {
114 return logic_.exportConsumers();
115 }
116
117 void
118 importConsumers(std::string const& origin, Gossip const& gossip) override
119 {
120 logic_.importConsumers(origin, gossip);
121 }
122
123 //--------------------------------------------------------------------------
124
126 getJson() override
127 {
128 return logic_.getJson();
129 }
130
132 getJson(int threshold) override
133 {
134 return logic_.getJson(threshold);
135 }
136
137 //--------------------------------------------------------------------------
138
139 void
141 {
142 logic_.onWrite(map);
143 }
144
145 //--------------------------------------------------------------------------
146
147private:
148 void
150 {
151 beast::setCurrentThreadName("Resource::Manager");
152 for (;;)
153 {
157 if (stop_)
158 break;
159 }
160 }
161};
162
163//------------------------------------------------------------------------------
164
165Manager::Manager() : beast::PropertyStream::Source("resource")
166{
167}
168
169Manager::~Manager() = default;
170
171//------------------------------------------------------------------------------
172
175 beast::insight::Collector::ptr const& collector,
176 beast::Journal journal)
177{
178 return std::make_unique<ManagerImp>(collector, journal);
179}
180
181} // namespace Resource
182} // namespace ripple
Represents a JSON value.
Definition: json_value.h:147
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
std::string to_string() const
Returns a string representing the endpoint.
Definition: IPEndpoint.cpp:57
A generic endpoint for log messages.
Definition: Journal.h:59
Stream warn() const
Definition: Journal.h:329
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 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:26
std::string_view forwardedFor(http_request_type const &request)
Definition: Role.cpp:261
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:120
static IP::Endpoint from_asio(boost::asio::ip::address const &address)
Data format for exchanging consumption information across peers.
Definition: Gossip.h:30