rippled
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 <ripple/basics/Log.h>
21 #include <ripple/basics/chrono.h>
22 #include <ripple/beast/core/CurrentThreadName.h>
23 #include <ripple/beast/net/IPAddressConversion.h>
24 #include <ripple/resource/ResourceManager.h>
25 #include <ripple/resource/impl/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 
33 namespace ripple {
34 namespace Resource {
35 
36 class ManagerImp : public Manager
37 {
38 private:
42  bool stop_ = false;
45 
46 public:
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;
57  ManagerImp&
58  operator=(ManagerImp const&) = delete;
59 
60  ~ManagerImp() override
61  {
62  {
63  std::lock_guard lock(mutex_);
64  stop_ = true;
65  cond_.notify_one();
66  }
67  thread_.join();
68  }
69 
70  Consumer
71  newInboundEndpoint(beast::IP::Endpoint const& address) override
72  {
73  return logic_.newInboundEndpoint(address);
74  }
75 
76  Consumer
78  beast::IP::Endpoint const& address,
79  bool const proxy,
80  boost::string_view const& forwardedFor) override
81  {
82  if (!proxy)
83  return newInboundEndpoint(address);
84 
85  boost::system::error_code ec;
86  auto const proxiedIp =
87  boost::asio::ip::make_address(forwardedFor.to_string(), ec);
88  if (ec)
89  {
90  journal_.warn()
91  << "forwarded for (" << forwardedFor << ") from proxy "
92  << address.to_string()
93  << " doesn't convert to IP endpoint: " << ec.message();
94  return newInboundEndpoint(address);
95  }
96  return newInboundEndpoint(
98  }
99 
100  Consumer
101  newOutboundEndpoint(beast::IP::Endpoint const& address) override
102  {
103  return logic_.newOutboundEndpoint(address);
104  }
105 
106  Consumer
107  newUnlimitedEndpoint(beast::IP::Endpoint const& address) override
108  {
109  return logic_.newUnlimitedEndpoint(address);
110  }
111 
112  Gossip
113  exportConsumers() override
114  {
115  return logic_.exportConsumers();
116  }
117 
118  void
119  importConsumers(std::string const& origin, Gossip const& gossip) override
120  {
121  logic_.importConsumers(origin, gossip);
122  }
123 
124  //--------------------------------------------------------------------------
125 
127  getJson() override
128  {
129  return logic_.getJson();
130  }
131 
133  getJson(int threshold) override
134  {
135  return logic_.getJson(threshold);
136  }
137 
138  //--------------------------------------------------------------------------
139 
140  void
142  {
143  logic_.onWrite(map);
144  }
145 
146  //--------------------------------------------------------------------------
147 
148 private:
149  void
150  run()
151  {
152  beast::setCurrentThreadName("Resource::Manager");
153  for (;;)
154  {
158  if (stop_)
159  break;
160  }
161  }
162 };
163 
164 //------------------------------------------------------------------------------
165 
166 Manager::Manager() : beast::PropertyStream::Source("resource")
167 {
168 }
169 
170 Manager::~Manager() = default;
171 
172 //------------------------------------------------------------------------------
173 
176  beast::insight::Collector::ptr const& collector,
177  beast::Journal journal)
178 {
179  return std::make_unique<ManagerImp>(collector, journal);
180 }
181 
182 } // namespace Resource
183 } // namespace ripple
ripple::Resource::Logic::getJson
Json::Value getJson()
Definition: resource/impl/Logic.h:201
ripple::Resource::ManagerImp
Definition: ResourceManager.cpp:36
ripple::Resource::Logic::onWrite
void onWrite(beast::PropertyStream::Map &map)
Definition: resource/impl/Logic.h:532
std::string
STL class.
std::shared_ptr< Collector >
beast::PropertyStream::Map
Definition: PropertyStream.h:224
ripple::Resource::ManagerImp::newInboundEndpoint
Consumer newInboundEndpoint(beast::IP::Endpoint const &address, bool const proxy, boost::string_view const &forwardedFor) override
Definition: ResourceManager.cpp:77
ripple::Resource::Gossip
Data format for exchanging consumption information across peers.
Definition: Gossip.h:29
beast::IP::Endpoint::to_string
std::string to_string() const
Returns a string representing the endpoint.
Definition: IPEndpoint.cpp:57
ripple::Resource::ManagerImp::mutex_
std::mutex mutex_
Definition: ResourceManager.cpp:43
ripple::Resource::Logic::newInboundEndpoint
Consumer newInboundEndpoint(beast::IP::Endpoint const &address)
Definition: resource/impl/Logic.h:110
ripple::Resource::ManagerImp::getJson
Json::Value getJson() override
Extract consumer information for reporting.
Definition: ResourceManager.cpp:127
std::chrono::seconds
ripple::Resource::ManagerImp::ManagerImp
ManagerImp(beast::insight::Collector::ptr const &collector, beast::Journal journal)
Definition: ResourceManager.cpp:47
ripple::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:71
beast::Journal::warn
Stream warn() const
Definition: Journal.h:327
std::lock_guard
STL class.
ripple::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:88
ripple::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:101
ripple::Resource::ManagerImp::importConsumers
void importConsumers(std::string const &origin, Gossip const &gossip) override
Import packaged consumer information.
Definition: ResourceManager.cpp:119
beast::IPAddressConversion::from_asio
static IP::Endpoint from_asio(boost::asio::ip::address const &address)
Definition: IPAddressConversion.h:63
ripple::Resource::Logic
Definition: resource/impl/Logic.h:40
ripple::Resource::ManagerImp::ManagerImp
ManagerImp()=delete
ripple::Resource::ManagerImp::journal_
const beast::Journal journal_
Definition: ResourceManager.cpp:39
ripple::Resource::ManagerImp::~ManagerImp
~ManagerImp() override
Definition: ResourceManager.cpp:60
std::thread
STL class.
ripple::Resource::Logic::importConsumers
void importConsumers(std::string const &origin, Gossip const &gossip)
Definition: resource/impl/Logic.h:282
ripple::Resource::ManagerImp::exportConsumers
Gossip exportConsumers() override
Extract packaged consumer information for export.
Definition: ResourceManager.cpp:113
std::unique_lock
STL class.
ripple::Resource::ManagerImp::stop_
bool stop_
Definition: ResourceManager.cpp:42
ripple::forwardedFor
boost::string_view forwardedFor(http_request_type const &request)
Definition: Role.cpp:261
ripple::Resource::ManagerImp::run
void run()
Definition: ResourceManager.cpp:150
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:58
memory
std::condition_variable::wait_for
T wait_for(T... args)
ripple::Resource::Manager::Manager
Manager()
Definition: ResourceManager.cpp:166
std::condition_variable::notify_one
T notify_one(T... args)
ripple::Resource::Manager
Tracks load and resource consumption.
Definition: ResourceManager.h:36
beast::setCurrentThreadName
void setCurrentThreadName(std::string_view name)
Changes the name of the caller thread.
Definition: CurrentThreadName.cpp:119
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::Resource::make_Manager
std::unique_ptr< Manager > make_Manager(beast::insight::Collector::ptr const &collector, beast::Journal journal)
Definition: ResourceManager.cpp:175
ripple::Resource::ManagerImp::logic_
Logic logic_
Definition: ResourceManager.cpp:40
ripple::Resource::Logic::newUnlimitedEndpoint
Consumer newUnlimitedEndpoint(beast::IP::Endpoint const &address)
Create endpoint that should not have resource limits applied.
Definition: resource/impl/Logic.h:173
ripple::Resource::ManagerImp::operator=
ManagerImp & operator=(ManagerImp const &)=delete
condition_variable
ripple::Resource::Consumer
An endpoint that consumes resources.
Definition: Consumer.h:34
ripple::Resource::ManagerImp::getJson
Json::Value getJson(int threshold) override
Definition: ResourceManager.cpp:133
mutex
ripple::Resource::Logic::exportConsumers
Gossip exportConsumers()
Definition: resource/impl/Logic.h:256
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:38
ripple::Resource::ManagerImp::thread_
std::thread thread_
Definition: ResourceManager.cpp:41
ripple::Resource::ManagerImp::onWrite
void onWrite(beast::PropertyStream::Map &map) override
Subclass override.
Definition: ResourceManager.cpp:141
ripple::Resource::ManagerImp::cond_
std::condition_variable cond_
Definition: ResourceManager.cpp:44
std::unique_ptr
STL class.
ripple::Resource::Logic::periodicActivity
void periodicActivity()
Definition: resource/impl/Logic.h:342
ripple::Resource::Logic::newOutboundEndpoint
Consumer newOutboundEndpoint(beast::IP::Endpoint const &address)
Definition: resource/impl/Logic.h:140
ripple::Resource::Manager::~Manager
virtual ~Manager()=0
ripple::Resource::ManagerImp::newUnlimitedEndpoint
Consumer newUnlimitedEndpoint(beast::IP::Endpoint const &address) override
Create a new unlimited endpoint keyed by forwarded IP.
Definition: ResourceManager.cpp:107
std::thread::join
T join(T... args)
Json::Value
Represents a JSON value.
Definition: json_value.h:145
beast
Definition: base_uint.h:641