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/resource/ResourceManager.h>
21 #include <ripple/resource/impl/Logic.h>
22 #include <ripple/basics/chrono.h>
23 #include <ripple/basics/Log.h>
24 #include <ripple/beast/core/CurrentThreadName.h>
25 #include <ripple/beast/net/IPAddressConversion.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::Journal journal)
49  : journal_ (journal)
50  , logic_ (collector, stopwatch(), journal)
51  {
53  }
54 
55  ManagerImp () = delete;
56  ManagerImp (ManagerImp const&) = delete;
57  ManagerImp& operator= (ManagerImp const&) = delete;
58 
59  ~ManagerImp () override
60  {
61  {
62  std::lock_guard lock(mutex_);
63  stop_ = true;
64  cond_.notify_one();
65  }
66  thread_.join();
67  }
68 
70  {
71  return logic_.newInboundEndpoint (address);
72  }
73 
75  bool const proxy, boost::string_view const& forwardedFor) override
76  {
77  if (! proxy)
78  return newInboundEndpoint(address);
79 
80  boost::system::error_code ec;
81  auto const proxiedIp = boost::asio::ip::make_address(
82  forwardedFor.to_string(), ec);
83  if (ec)
84  {
85  journal_.warn() << "forwarded for ("
86  << forwardedFor
87  << ") from proxy "
88  << address.to_string()
89  << " doesn't convert to IP endpoint: "
90  << ec.message();
91  return newInboundEndpoint(address);
92  }
93  return newInboundEndpoint(
95  }
96 
98  {
99  return logic_.newOutboundEndpoint (address);
100  }
101 
103  {
104  return logic_.newUnlimitedEndpoint (address);
105  }
106 
108  {
109  return logic_.exportConsumers();
110  }
111 
113  std::string const& origin, Gossip const& gossip) override
114  {
115  logic_.importConsumers (origin, gossip);
116  }
117 
118  //--------------------------------------------------------------------------
119 
120  Json::Value getJson () override
121  {
122  return logic_.getJson ();
123  }
124 
125  Json::Value getJson (int threshold) override
126  {
127  return logic_.getJson (threshold);
128  }
129 
130  //--------------------------------------------------------------------------
131 
132  void onWrite (beast::PropertyStream::Map& map) override
133  {
134  logic_.onWrite (map);
135  }
136 
137  //--------------------------------------------------------------------------
138 
139 private:
140  void run ()
141  {
142  beast::setCurrentThreadName ("Resource::Manager");
143  for(;;)
144  {
148  if (stop_)
149  break;
150  }
151  }
152 };
153 
154 //------------------------------------------------------------------------------
155 
157  : beast::PropertyStream::Source ("resource")
158 {
159 }
160 
161 Manager::~Manager() = default;
162 
163 //------------------------------------------------------------------------------
164 
166  beast::insight::Collector::ptr const& collector,
167  beast::Journal journal)
168 {
169  return std::make_unique <ManagerImp> (collector, journal);
170 }
171 
172 }
173 }
ripple::Resource::Logic::getJson
Json::Value getJson()
Definition: resource/impl/Logic.h:204
ripple::Resource::ManagerImp
Definition: ResourceManager.cpp:36
ripple::Resource::Logic::onWrite
void onWrite(beast::PropertyStream::Map &map)
Definition: resource/impl/Logic.h:526
std::string
STL class.
std::shared_ptr< Collector >
beast::PropertyStream::Map
Definition: PropertyStream.h:185
ripple::Resource::ManagerImp::newInboundEndpoint
Consumer newInboundEndpoint(beast::IP::Endpoint const &address, bool const proxy, boost::string_view const &forwardedFor) override
Definition: ResourceManager.cpp:74
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:54
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:120
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:69
beast::Journal::warn
Stream warn() const
Definition: Journal.h:302
std::lock_guard
STL class.
ripple::stopwatch
Stopwatch & stopwatch()
Returns an instance of a wall clock.
Definition: chrono.h:87
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:97
ripple::Resource::ManagerImp::importConsumers
void importConsumers(std::string const &origin, Gossip const &gossip) override
Import packaged consumer information.
Definition: ResourceManager.cpp:112
beast::IPAddressConversion::from_asio
static IP::Endpoint from_asio(boost::asio::ip::address const &address)
Definition: IPAddressConversion.h:58
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:59
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:107
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:112
ripple::Resource::ManagerImp::run
void run()
Definition: ResourceManager.cpp:140
beast::Journal
A generic endpoint for log messages.
Definition: Journal.h:60
memory
std::condition_variable::wait_for
T wait_for(T... args)
ripple::Resource::Manager::Manager
Manager()
Definition: ResourceManager.cpp:156
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:113
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:165
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:175
ripple::Resource::ManagerImp::operator=
ManagerImp & operator=(ManagerImp const &)=delete
condition_variable
ripple::Resource::Consumer
An endpoint that consumes resources.
Definition: Consumer.h:33
ripple::Resource::ManagerImp::getJson
Json::Value getJson(int threshold) override
Definition: ResourceManager.cpp:125
mutex
ripple::Resource::Logic::exportConsumers
Gossip exportConsumers()
Definition: resource/impl/Logic.h:257
beast::IP::Endpoint
A version-independent IP address and port combination.
Definition: IPEndpoint.h:39
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:132
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:340
ripple::Resource::Logic::newOutboundEndpoint
Consumer newOutboundEndpoint(beast::IP::Endpoint const &address)
Definition: resource/impl/Logic.h:141
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:102
std::thread::join
T join(T... args)
Json::Value
Represents a JSON value.
Definition: json_value.h:141
beast
Definition: base_uint.h:582