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/StringHash.hpp"
24#include "util/Taggable.hpp"
25#include "util/log/Logger.hpp"
26#include "util/prometheus/Gauge.hpp"
27#include "util/prometheus/Label.hpp"
28#include "util/prometheus/Prometheus.hpp"
29#include "web/ProxyIpResolver.hpp"
30#include "web/SubscriptionContextInterface.hpp"
31#include "web/ng/Connection.hpp"
32#include "web/ng/Error.hpp"
33#include "web/ng/MessageHandler.hpp"
34#include "web/ng/ProcessingPolicy.hpp"
35#include "web/ng/Request.hpp"
36#include "web/ng/Response.hpp"
37
38#include <boost/asio/spawn.hpp>
39#include <boost/signals2/signal.hpp>
40#include <boost/signals2/variadic_signal.hpp>
41
42#include <atomic>
43#include <chrono>
44#include <cstddef>
45#include <functional>
46#include <memory>
47#include <optional>
48#include <string>
49#include <unordered_map>
50
51namespace web::ng::impl {
52
54public:
55 using OnDisconnectHook = std::function<void(Connection const&)>;
56 using OnIpChangeHook = std::function<void(std::string const&, std::string const&)>;
57 using TargetToHandlerMap = std::unordered_map<std::string, MessageHandler, util::StringHash, std::equal_to<>>;
58
59private:
60 util::Logger log_{"WebServer"};
61 util::Logger perfLog_{"Performance"};
62
63 ProcessingPolicy processingPolicy_;
64 std::optional<size_t> maxParallelRequests_;
65
66 std::reference_wrapper<util::TagDecoratorFactory> tagFactory_;
67 std::optional<size_t> maxSubscriptionSendQueueSize_;
68
69 ProxyIpResolver proxyIpResolver_;
70
71 OnDisconnectHook onDisconnectHook_;
72 OnIpChangeHook onIpChangeHook_;
73
74 TargetToHandlerMap getHandlers_;
75 TargetToHandlerMap postHandlers_;
76 std::optional<MessageHandler> wsHandler_;
77
78 boost::signals2::signal<void()> onStop_;
79 std::unique_ptr<std::atomic_bool> stopping_ = std::make_unique<std::atomic_bool>(false);
80
81 std::reference_wrapper<util::prometheus::GaugeInt> connectionsCounter_ =
82 PrometheusService::gaugeInt("connections_total_number", util::prometheus::Labels{{{"status", "connected"}}});
83
84 util::StopHelper stopHelper_;
85
86public:
88 ProcessingPolicy processingPolicy,
89 std::optional<size_t> maxParallelRequests,
90 util::TagDecoratorFactory& tagFactory,
91 std::optional<size_t> maxSubscriptionSendQueueSize,
92 ProxyIpResolver proxyIpResolver,
93 OnDisconnectHook onDisconnectHook,
94 OnIpChangeHook onIpChangeHook
95 );
96
98
99 static constexpr std::chrono::milliseconds kCLOSE_CONNECTION_TIMEOUT{500};
100
101 void
102 onGet(std::string const& target, MessageHandler handler);
103
104 void
105 onPost(std::string const& target, MessageHandler handler);
106
107 void
108 onWs(MessageHandler handler);
109
110 void
111 processConnection(ConnectionPtr connection, boost::asio::yield_context yield);
112
113 static void
114 stopConnection(Connection& connection, boost::asio::yield_context yield);
115
116 void
117 stop(boost::asio::yield_context yield);
118
119 bool
120 isStopping() const;
121
122private:
130 bool
131 handleError(Error const& error, Connection const& connection) const;
132
140 bool
141 sequentRequestResponseLoop(
142 Connection& connection,
143 SubscriptionContextPtr& subscriptionContext,
144 boost::asio::yield_context yield
145 );
146
147 bool
148 parallelRequestResponseLoop(
149 Connection& connection,
150 SubscriptionContextPtr& subscriptionContext,
151 boost::asio::yield_context yield
152 );
153
154 std::optional<bool>
155 processRequest(
156 Connection& connection,
157 SubscriptionContextPtr& subscriptionContext,
158 Request const& request,
159 boost::asio::yield_context yield
160 );
161
163 handleRequest(
164 ConnectionMetadata& connectionMetadata,
165 SubscriptionContextPtr& subscriptionContext,
166 Request const& request,
167 boost::asio::yield_context yield
168 );
169
170 void
171 resolveClientIp(Connection& connection, Request const& request) const;
172};
173
174} // 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:216
A simple thread-safe logger for the channel specified in the constructor.
Definition Logger.hpp:87
Helper class to stop a class asynchronously.
Definition StopHelper.hpp:34
A factory for TagDecorator instantiation.
Definition Taggable.hpp:182
Class representing a collection of Prometheus labels.
Definition Label.hpp:59
Resolves the client's IP address, considering proxy servers.
Definition ProxyIpResolver.hpp:44
An interface for a connection metadata class.
Definition Connection.hpp:44
A class representing a connection to a client.
Definition Connection.hpp:112
Represents an HTTP or WebSocket request.
Definition Request.hpp:37
Represents an HTTP or Websocket response.
Definition Response.hpp:40
Definition ConnectionHandler.hpp:53
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:83