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 ConnectionHandler(ConnectionHandler&&) = delete;
104
105 static constexpr std::chrono::milliseconds kCLOSE_CONNECTION_TIMEOUT{500};
106
107 void
108 onGet(std::string const& target, MessageHandler handler);
109
110 void
111 onPost(std::string const& target, MessageHandler handler);
112
113 void
114 onWs(MessageHandler handler);
115
116 void
117 processConnection(ConnectionPtr connection, boost::asio::yield_context yield);
118
119 static void
120 stopConnection(Connection& connection, boost::asio::yield_context yield);
121
122 void
123 stop(boost::asio::yield_context yield);
124
125 bool
126 isStopping() const;
127
128private:
136 bool
137 handleError(Error const& error, Connection const& connection) const;
138
146 bool
147 sequentRequestResponseLoop(
148 Connection& connection,
149 SubscriptionContextPtr& subscriptionContext,
150 boost::asio::yield_context yield
151 );
152
153 bool
154 parallelRequestResponseLoop(
155 Connection& connection,
156 SubscriptionContextPtr& subscriptionContext,
157 boost::asio::yield_context yield
158 );
159
160 std::optional<bool>
161 processRequest(
162 Connection& connection,
163 SubscriptionContextPtr& subscriptionContext,
164 Request const& request,
165 boost::asio::yield_context yield
166 );
167
168 Response
169 handleRequest(
170 ConnectionMetadata& connectionMetadata,
171 SubscriptionContextPtr& subscriptionContext,
172 Request const& request,
173 boost::asio::yield_context yield
174 );
175};
176
177} // 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:111
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