22#include "data/BackendInterface.hpp"
26#include "rpc/WorkQueue.hpp"
27#include "rpc/common/HandlerProvider.hpp"
28#include "rpc/common/Types.hpp"
29#include "rpc/common/impl/ForwardingProxy.hpp"
30#include "util/OverloadSet.hpp"
31#include "util/ResponseExpirationCache.hpp"
32#include "util/log/Logger.hpp"
33#include "web/Context.hpp"
34#include "web/dosguard/DOSGuardInterface.hpp"
36#include <boost/asio/spawn.hpp>
37#include <boost/iterator/transform_iterator.hpp>
38#include <boost/json.hpp>
39#include <boost/json/object.hpp>
40#include <fmt/format.h>
41#include <xrpl/protocol/ErrorCodes.h>
49#include <unordered_set>
60template <
typename CountersType>
65 std::shared_ptr<BackendInterface> backend_;
66 std::reference_wrapper<web::dosguard::DOSGuardInterface const> dosGuard_;
67 std::reference_wrapper<WorkQueue> workQueue_;
68 std::reference_wrapper<CountersType> counters_;
70 std::shared_ptr<HandlerProvider const> handlerProvider_;
74 std::optional<util::ResponseExpirationCache> responseCache_;
90 std::shared_ptr<BackendInterface>
const& backend,
91 std::shared_ptr<etlng::LoadBalancerInterface>
const& balancer,
94 CountersType& counters,
95 std::shared_ptr<HandlerProvider const>
const& handlerProvider
98 , dosGuard_{std::cref(dosGuard)}
99 , workQueue_{std::ref(workQueue)}
100 , counters_{std::ref(counters)}
101 , handlerProvider_{handlerProvider}
102 , forwardingProxy_{balancer, counters, handlerProvider}
105 auto const cacheTimeout = config.
get<
float>(
"rpc.cache_timeout");
107 if (cacheTimeout > 0.f) {
108 LOG(log_.
info()) << fmt::format(
"Init RPC Cache, timeout: {} seconds", cacheTimeout);
110 responseCache_.emplace(
112 std::unordered_set<std::string>{
"server_info"}
129 static std::shared_ptr<RPCEngine>
132 std::shared_ptr<BackendInterface>
const& backend,
133 std::shared_ptr<etlng::LoadBalancerInterface>
const& balancer,
136 CountersType& counters,
137 std::shared_ptr<HandlerProvider const>
const& handlerProvider
140 return std::make_shared<RPCEngine>(config, backend, balancer, dosGuard, workQueue, counters, handlerProvider);
152 if (forwardingProxy_.shouldForward(ctx)) {
157 return forwardingProxy_.forward(ctx);
160 if (not ctx.isAdmin and responseCache_) {
161 if (
auto res = responseCache_->get(ctx.method); res.has_value())
162 return Result{std::move(res).value()};
165 if (backend_->isTooBusy()) {
166 LOG(log_.
error()) <<
"Database is too busy. Rejecting request";
171 auto const method = handlerProvider_->getHandler(ctx.method);
174 return Result{
Status{RippledError::rpcUNKNOWN_COMMAND}};
178 LOG(perfLog_.
debug()) << ctx.
tag() <<
" start executing rpc `" << ctx.method <<
'`';
182 .session = ctx.session,
183 .isAdmin = ctx.isAdmin,
184 .clientIp = ctx.clientIp,
185 .apiVersion = ctx.apiVersion
187 auto v = (*method).process(ctx.params, context);
189 LOG(perfLog_.
debug()) << ctx.
tag() <<
" finish executing rpc `" << ctx.method <<
'`';
193 }
else if (not ctx.isAdmin and responseCache_) {
194 responseCache_->put(ctx.method, v.result->as_object());
197 return Result{std::move(v)};
199 LOG(log_.
error()) <<
"Database timeout";
203 }
catch (std::exception
const& ex) {
204 LOG(log_.
error()) << ctx.
tag() <<
"Caught exception: " << ex.what();
219 template <
typename FnType>
221 post(FnType&& func, std::string
const& ip)
223 return workQueue_.get().postCoro(std::forward<FnType>(func), dosGuard_.get().isWhiteListed(ip));
233 notifyComplete(std::string
const& method, std::chrono::microseconds
const& duration)
235 if (validHandler(method))
236 counters_.get().rpcComplete(method, duration);
250 if (validHandler(method))
251 counters_.get().rpcFailed(method);
264 if (validHandler(method))
265 counters_.get().rpcErrored(method);
274 counters_.get().onTooBusy();
285 counters_.get().onNotReady();
294 counters_.get().onBadSyntax();
303 counters_.get().onUnknownCommand();
312 counters_.get().onInternalError();
317 validHandler(std::string
const& method)
const
319 return handlerProvider_->contains(method) || forwardingProxy_.isProxied(method);
325 if (backend_->isTooBusy()) {
326 LOG(log_.
error()) <<
"Database is too busy. Rejecting request";
328 return Result{Status{RippledError::rpcTOO_BUSY}};
331 auto const method = handlerProvider_->getHandler(ctx.method);
334 return Result{Status{RippledError::rpcUNKNOWN_COMMAND}};
338 LOG(perfLog_.
debug()) << ctx.
tag() <<
" start executing rpc `" << ctx.method <<
'`';
340 auto const context = Context{
342 .session = ctx.session,
343 .isAdmin = ctx.isAdmin,
344 .clientIp = ctx.clientIp,
345 .apiVersion = ctx.apiVersion
347 auto v = (*method).process(ctx.params, context);
349 LOG(perfLog_.
debug()) << ctx.
tag() <<
" finish executing rpc `" << ctx.method <<
'`';
355 return Result{std::move(v)};
357 LOG(log_.
error()) <<
"Database timeout";
360 return Result{Status{RippledError::rpcTOO_BUSY}};
361 }
catch (std::exception
const& ex) {
362 LOG(log_.
error()) << ctx.
tag() <<
"Caught exception: " << ex.what();
365 return Result{Status{RippledError::rpcINTERNAL}};
Represents a database timeout error.
Definition BackendInterface.hpp:59
The RPC engine that ties all RPC-related functionality together.
Definition RPCEngine.hpp:61
void notifyNotReady()
Notify the system that the RPC system was not ready to handle an incoming request.
Definition RPCEngine.hpp:283
RPCEngine(util::config::ClioConfigDefinition const &config, std::shared_ptr< BackendInterface > const &backend, std::shared_ptr< etlng::LoadBalancerInterface > const &balancer, web::dosguard::DOSGuardInterface const &dosGuard, WorkQueue &workQueue, CountersType &counters, std::shared_ptr< HandlerProvider const > const &handlerProvider)
Construct a new RPCEngine object.
Definition RPCEngine.hpp:88
void notifyInternalError()
Notify the system that the incoming request lead to an internal error (unrecoverable).
Definition RPCEngine.hpp:310
void notifyErrored(std::string const &method)
Notify the system that specified method failed due to some unrecoverable error.
Definition RPCEngine.hpp:262
void notifyUnknownCommand()
Notify the system that the incoming request specified an unknown/unsupported method/command.
Definition RPCEngine.hpp:301
void notifyBadSyntax()
Notify the system that the incoming request did not specify the RPC method/command.
Definition RPCEngine.hpp:292
void notifyFailed(std::string const &method)
Notify the system that specified method failed to execute due to a recoverable user error.
Definition RPCEngine.hpp:247
void notifyTooBusy()
Notify the system that the RPC system is too busy to handle an incoming request.
Definition RPCEngine.hpp:272
void notifyComplete(std::string const &method, std::chrono::microseconds const &duration)
Notify the system that specified method was executed.
Definition RPCEngine.hpp:233
Result buildResponse(web::Context const &ctx)
Main request processor routine.
Definition RPCEngine.hpp:150
static std::shared_ptr< RPCEngine > makeRPCEngine(util::config::ClioConfigDefinition const &config, std::shared_ptr< BackendInterface > const &backend, std::shared_ptr< etlng::LoadBalancerInterface > const &balancer, web::dosguard::DOSGuardInterface const &dosGuard, WorkQueue &workQueue, CountersType &counters, std::shared_ptr< HandlerProvider const > const &handlerProvider)
Factory function to create a new instance of the RPC engine.
Definition RPCEngine.hpp:130
bool post(FnType &&func, std::string const &ip)
Used to schedule request processing onto the work queue.
Definition RPCEngine.hpp:221
An asynchronous, thread-safe queue for RPC requests.
Definition WorkQueue.hpp:48
Definition ForwardingProxy.hpp:41
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:111
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:322
Pump debug(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::DBG severity.
Definition Logger.cpp:307
Pump info(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::NFO severity.
Definition Logger.cpp:312
BaseTagDecorator const & tag() const
Getter for tag decorator.
Definition Taggable.hpp:280
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:54
static std::chrono::milliseconds toMilliseconds(float value)
Method to convert a float seconds value to milliseconds.
Definition ConfigDefinition.cpp:121
T get(std::string_view fullKey) const
Returns the specified value of given string if value exists.
Definition ConfigDefinition.hpp:108
The interface of a denial of service guard.
Definition DOSGuardInterface.hpp:46
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:37
bool isAdminCmd(std::string const &method, boost::json::object const &request)
Check whether a request requires administrative privileges on rippled side.
Definition RPCHelpers.cpp:1518
Context of an RPC call.
Definition Types.hpp:118
Result type used to return responses or error statuses to the Webserver subsystem.
Definition Types.hpp:129
A status returned from any RPC handler.
Definition Errors.hpp:83
Context that is used by the Webserver to pass around information about an incoming request.
Definition Context.hpp:40