Clio develop
The XRP Ledger API server.
Loading...
Searching...
No Matches
ConnectionHandler.hpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of clio: https://github.com/XRPLF/clio
4 Copyright (c) 2024, the clio developers.
5
6 Permission to use, copy, modify, and 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#pragma once
21
22#include "util/StopHelper.hpp"
23#include "util/Taggable.hpp"
24#include "util/log/Logger.hpp"
25#include "util/prometheus/Gauge.hpp"
26#include "util/prometheus/Label.hpp"
27#include "util/prometheus/Prometheus.hpp"
28#include "web/SubscriptionContextInterface.hpp"
29#include "web/ng/Connection.hpp"
30#include "web/ng/Error.hpp"
31#include "web/ng/MessageHandler.hpp"
32#include "web/ng/ProcessingPolicy.hpp"
33#include "web/ng/Request.hpp"
34#include "web/ng/Response.hpp"
35
36#include <boost/asio/spawn.hpp>
37#include <boost/signals2/signal.hpp>
38#include <boost/signals2/variadic_signal.hpp>
39
40#include <atomic>
41#include <chrono>
42#include <cstddef>
43#include <functional>
44#include <memory>
45#include <optional>
46#include <string>
47#include <string_view>
48#include <unordered_map>
49
50namespace web::ng::impl {
51
53public:
54 using OnDisconnectHook = std::function<void(Connection const&)>;
55
56 struct StringHash {
57 using hash_type = std::hash<std::string_view>;
58 using is_transparent = void;
59
60 std::size_t
61 operator()(char const* str) const;
62 std::size_t
63 operator()(std::string_view str) const;
64 std::size_t
65 operator()(std::string const& str) const;
66 };
67
68 using TargetToHandlerMap = std::unordered_map<std::string, MessageHandler, StringHash, std::equal_to<>>;
69
70private:
71 util::Logger log_{"WebServer"};
72 util::Logger perfLog_{"Performance"};
73
74 ProcessingPolicy processingPolicy_;
75 std::optional<size_t> maxParallelRequests_;
76
77 std::reference_wrapper<util::TagDecoratorFactory> tagFactory_;
78 std::optional<size_t> maxSubscriptionSendQueueSize_;
79
80 OnDisconnectHook onDisconnectHook_;
81
82 TargetToHandlerMap getHandlers_;
83 TargetToHandlerMap postHandlers_;
84 std::optional<MessageHandler> wsHandler_;
85
86 boost::signals2::signal<void()> onStop_;
87 std::unique_ptr<std::atomic_bool> stopping_ = std::make_unique<std::atomic_bool>(false);
88
89 std::reference_wrapper<util::prometheus::GaugeInt> connectionsCounter_ =
90 PrometheusService::gaugeInt("connections_total_number", util::prometheus::Labels{{{"status", "connected"}}});
91
92 util::StopHelper stopHelper_;
93
94public:
95 ConnectionHandler(
96 ProcessingPolicy processingPolicy,
97 std::optional<size_t> maxParallelRequests,
98 util::TagDecoratorFactory& tagFactory,
99 std::optional<size_t> maxSubscriptionSendQueueSize,
100 OnDisconnectHook onDisconnectHook
101 );
102
103 static constexpr std::chrono::milliseconds kCLOSE_CONNECTION_TIMEOUT{500};
104
105 void
106 onGet(std::string const& target, MessageHandler handler);
107
108 void
109 onPost(std::string const& target, MessageHandler handler);
110
111 void
112 onWs(MessageHandler handler);
113
114 void
115 processConnection(ConnectionPtr connection, boost::asio::yield_context yield);
116
117 static void
118 stopConnection(Connection& connection, boost::asio::yield_context yield);
119
120 void
121 stop(boost::asio::yield_context yield);
122
123 bool
124 isStopping() const;
125
126private:
134 bool
135 handleError(Error const& error, Connection const& connection) const;
136
144 bool
145 sequentRequestResponseLoop(
146 Connection& connection,
147 SubscriptionContextPtr& subscriptionContext,
148 boost::asio::yield_context yield
149 );
150
151 bool
152 parallelRequestResponseLoop(
153 Connection& connection,
154 SubscriptionContextPtr& subscriptionContext,
155 boost::asio::yield_context yield
156 );
157
158 std::optional<bool>
159 processRequest(
160 Connection& connection,
161 SubscriptionContextPtr& subscriptionContext,
162 Request const& request,
163 boost::asio::yield_context yield
164 );
165
166 Response
167 handleRequest(
168 ConnectionMetadata& connectionMetadata,
169 SubscriptionContextPtr& subscriptionContext,
170 Request const& request,
171 boost::asio::yield_context yield
172 );
173};
174
175} // namespace web::ng::impl
static util::prometheus::GaugeInt & gaugeInt(std::string name, util::prometheus::Labels labels, std::optional< std::string > description=std::nullopt)
Get an integer based gauge metric. It will be created if it doesn't exist.
Definition Prometheus.cpp:210
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:110
Helper class to stop a class asynchronously.
Definition StopHelper.hpp:34
A factory for TagDecorator instantiation.
Definition Taggable.hpp:169
Class representing a collection of Prometheus labels.
Definition Label.hpp:59
A class representing a connection to a client.
Definition Connection.hpp:100
Definition ConnectionHandler.hpp:52
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:86
Definition ConnectionHandler.hpp:56