3#include "data/BackendInterface.hpp"
7#include "rpc/WorkQueue.hpp"
8#include "rpc/common/HandlerProvider.hpp"
9#include "rpc/common/Types.hpp"
10#include "rpc/common/impl/ForwardingProxy.hpp"
11#include "util/ResponseExpirationCache.hpp"
12#include "util/log/Logger.hpp"
13#include "web/Context.hpp"
14#include "web/dosguard/DOSGuardInterface.hpp"
16#include <boost/asio/spawn.hpp>
17#include <boost/iterator/transform_iterator.hpp>
18#include <boost/json.hpp>
19#include <boost/json/object.hpp>
20#include <fmt/format.h>
21#include <xrpl/protocol/ErrorCodes.h>
30#include <unordered_set>
41template <
typename CountersType>
46 std::shared_ptr<BackendInterface> backend_;
47 std::reference_wrapper<web::dosguard::DOSGuardInterface const> dosGuard_;
48 std::reference_wrapper<WorkQueue> workQueue_;
49 std::reference_wrapper<CountersType> counters_;
51 std::shared_ptr<HandlerProvider const> handlerProvider_;
55 std::optional<util::ResponseExpirationCache> responseCache_;
71 std::shared_ptr<BackendInterface> backend,
72 std::shared_ptr<etl::LoadBalancerInterface>
const& balancer,
75 CountersType& counters,
76 std::shared_ptr<HandlerProvider const>
const& handlerProvider
78 : backend_{std::move(backend)}
79 , dosGuard_{std::cref(dosGuard)}
80 , workQueue_{std::ref(workQueue)}
81 , counters_{std::ref(counters)}
82 , handlerProvider_{handlerProvider}
83 , forwardingProxy_{balancer, counters, handlerProvider}
86 auto const cacheTimeout = config.
get<
float>(
"rpc.cache_timeout");
88 if (cacheTimeout > 0.f) {
89 LOG(log_.info()) << fmt::format(
"Init RPC Cache, timeout: {} seconds", cacheTimeout);
91 responseCache_.emplace(
93 std::unordered_set<std::string>{
"server_info"}
110 static std::shared_ptr<RPCEngine>
113 std::shared_ptr<BackendInterface>
const& backend,
114 std::shared_ptr<etl::LoadBalancerInterface>
const& balancer,
117 CountersType& counters,
118 std::shared_ptr<HandlerProvider const>
const& handlerProvider
121 return std::make_shared<RPCEngine>(
122 config, backend, balancer, dosGuard, workQueue, counters, handlerProvider
135 if (forwardingProxy_.shouldForward(ctx)) {
140 return forwardingProxy_.forward(ctx);
143 if (not ctx.isAdmin and responseCache_) {
144 if (
auto res = responseCache_->get(ctx.method); res.has_value())
145 return Result{std::move(res).value()};
148 if (backend_->isTooBusy()) {
149 LOG(log_.error()) <<
"Database is too busy. Rejecting request";
154 auto const method = handlerProvider_->getHandler(ctx.method);
157 return Result{
Status{RippledError::rpcUNKNOWN_COMMAND}};
161 LOG(perfLog_.debug()) << ctx.
tag() <<
" start executing rpc `" << ctx.method <<
'`';
165 .session = ctx.session,
166 .isAdmin = ctx.isAdmin,
167 .clientIp = ctx.clientIp,
168 .apiVersion = ctx.apiVersion
170 auto v = (*method).process(ctx.params, context);
172 LOG(perfLog_.debug()) << ctx.
tag() <<
" finish executing rpc `" << ctx.method <<
'`';
176 }
else if (not ctx.isAdmin and responseCache_) {
177 responseCache_->put(ctx.method, v.result->as_object());
180 return Result{std::move(v)};
182 LOG(log_.error()) <<
"Database timeout";
186 }
catch (std::exception
const& ex) {
187 LOG(log_.error()) << ctx.
tag() <<
"Caught exception: " << ex.what();
202 template <
typename FnType>
204 post(FnType&& func, std::string
const& ip)
206 return workQueue_.get().postCoro(
207 std::forward<FnType>(func), dosGuard_.get().isWhiteListed(ip)
221 std::chrono::microseconds
const& duration,
225 if (validHandler(context.method)) {
226 counters_.get().rpcComplete(context.method, duration);
227 if (not isForwarded) {
228 counters_.get().recordLedgerRequest(context.params, context.range.maxSequence);
242 counters_.get().recordLedgerRequest(params, currentLedgerSequence);
257 if (validHandler(method))
258 counters_.get().rpcFailed(method);
271 if (validHandler(method))
272 counters_.get().rpcErrored(method);
281 counters_.get().onTooBusy();
292 counters_.get().onNotReady();
301 counters_.get().onBadSyntax();
311 counters_.get().onUnknownCommand();
320 counters_.get().onInternalError();
325 validHandler(std::string
const& method)
const
327 return handlerProvider_->contains(method) || forwardingProxy_.isProxied(method);
333 if (backend_->isTooBusy()) {
334 LOG(log_.
error()) <<
"Database is too busy. Rejecting request";
336 return Result{Status{RippledError::rpcTOO_BUSY}};
339 auto const method = handlerProvider_->getHandler(ctx.method);
342 return Result{Status{RippledError::rpcUNKNOWN_COMMAND}};
346 LOG(perfLog_.debug()) << ctx.
tag() <<
" start executing rpc `" << ctx.method <<
'`';
348 auto const context = Context{
350 .session = ctx.session,
351 .isAdmin = ctx.isAdmin,
352 .clientIp = ctx.clientIp,
353 .apiVersion = ctx.apiVersion
355 auto v = (*method).process(ctx.params, context);
357 LOG(perfLog_.debug()) << ctx.
tag() <<
" finish executing rpc `" << ctx.method <<
'`';
363 return Result{std::move(v)};
364 }
catch (data::DatabaseTimeout
const& t) {
365 LOG(log_.error()) <<
"Database timeout";
368 return Result{Status{RippledError::rpcTOO_BUSY}};
369 }
catch (std::exception
const& ex) {
370 LOG(log_.error()) << ctx.
tag() <<
"Caught exception: " << ex.what();
373 return Result{Status{RippledError::rpcINTERNAL}};
Represents a database timeout error.
Definition BackendInterface.hpp:40
void notifyNotReady()
Notify the system that the RPC system was not ready to handle an incoming request.
Definition RPCEngine.hpp:290
void notifyInternalError()
Notify the system that the incoming request lead to an internal error (unrecoverable).
Definition RPCEngine.hpp:318
void notifyErrored(std::string const &method)
Notify the system that specified method failed due to some unrecoverable error.
Definition RPCEngine.hpp:269
void notifyUnknownCommand()
Notify the system that the incoming request specified an unknown/unsupported method/command.
Definition RPCEngine.hpp:309
static std::shared_ptr< RPCEngine > makeRPCEngine(util::config::ClioConfigDefinition const &config, std::shared_ptr< BackendInterface > const &backend, std::shared_ptr< etl::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:111
RPCEngine(util::config::ClioConfigDefinition const &config, std::shared_ptr< BackendInterface > backend, std::shared_ptr< etl::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:69
void notifyBadSyntax()
Notify the system that the incoming request did not specify the RPC method/command.
Definition RPCEngine.hpp:299
void recordLedgerMetrics(boost::json::object const ¶ms, std::uint32_t currentLedgerSequence)
Record ledger request metrics.
Definition RPCEngine.hpp:240
void notifyFailed(std::string const &method)
Notify the system that specified method failed to execute due to a recoverable user error.
Definition RPCEngine.hpp:254
void notifyComplete(web::Context const &context, std::chrono::microseconds const &duration, bool isForwarded)
Notify the system that specified method was executed and record ledger metrics.
Definition RPCEngine.hpp:219
void notifyTooBusy()
Notify the system that the RPC system is too busy to handle an incoming request.
Definition RPCEngine.hpp:279
Result buildResponse(web::Context const &ctx)
Main request processor routine.
Definition RPCEngine.hpp:133
bool post(FnType &&func, std::string const &ip)
Used to schedule request processing onto the work queue.
Definition RPCEngine.hpp:204
An asynchronous, thread-safe queue for RPC requests.
Definition WorkQueue.hpp:43
Definition ForwardingProxy.hpp:22
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:77
Pump error(SourceLocationType const &loc=CURRENT_SRC_LOCATION) const
Interface for logging at Severity::ERR severity.
Definition Logger.cpp:498
BaseTagDecorator const & tag() const
Getter for tag decorator.
Definition Taggable.hpp:264
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
static std::chrono::milliseconds toMilliseconds(float value)
Method to convert a float seconds value to milliseconds.
Definition ConfigDefinition.cpp:109
T get(std::string_view fullKey) const
Returns the specified value of given string if value exists.
Definition ConfigDefinition.hpp:85
The interface of a denial of service guard.
Definition DOSGuardInterface.hpp:27
This namespace contains all the RPC logic and handlers.
Definition AMMHelpers.cpp:18
bool isAdminCmd(std::string const &method, boost::json::object const &request)
Check whether a request requires administrative privileges on rippled side.
Definition RPCHelpers.cpp:1604
Context of an RPC call.
Definition Types.hpp:99
Result type used to return responses or error statuses to the Webserver subsystem.
Definition Types.hpp:110
A status returned from any RPC handler.
Definition Errors.hpp:65
Context that is used by the Webserver to pass around information about an incoming request.
Definition Context.hpp:22