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/SubscriptionContextInterface.hpp"
30#include "web/ng/Connection.hpp"
31#include "web/ng/Error.hpp"
32#include "web/ng/MessageHandler.hpp"
33#include "web/ng/ProcessingPolicy.hpp"
34#include "web/ng/Request.hpp"
35#include "web/ng/Response.hpp"
36
37#include <boost/asio/spawn.hpp>
38#include <boost/signals2/signal.hpp>
39#include <boost/signals2/variadic_signal.hpp>
40
41#include <atomic>
42#include <chrono>
43#include <cstddef>
44#include <functional>
45#include <memory>
46#include <optional>
47#include <string>
48#include <unordered_map>
49
50namespace web::ng::impl {
51
53public:
54 using OnDisconnectHook = std::function<void(Connection const&)>;
55 using TargetToHandlerMap = std::unordered_map<std::string, MessageHandler, util::StringHash, std::equal_to<>>;
56
57private:
58 util::Logger log_{"WebServer"};
59 util::Logger perfLog_{"Performance"};
60
61 ProcessingPolicy processingPolicy_;
62 std::optional<size_t> maxParallelRequests_;
63
64 std::reference_wrapper<util::TagDecoratorFactory> tagFactory_;
65 std::optional<size_t> maxSubscriptionSendQueueSize_;
66
67 OnDisconnectHook onDisconnectHook_;
68
69 TargetToHandlerMap getHandlers_;
70 TargetToHandlerMap postHandlers_;
71 std::optional<MessageHandler> wsHandler_;
72
73 boost::signals2::signal<void()> onStop_;
74 std::unique_ptr<std::atomic_bool> stopping_ = std::make_unique<std::atomic_bool>(false);
75
76 std::reference_wrapper<util::prometheus::GaugeInt> connectionsCounter_ =
77 PrometheusService::gaugeInt("connections_total_number", util::prometheus::Labels{{{"status", "connected"}}});
78
79 util::StopHelper stopHelper_;
80
81public:
83 ProcessingPolicy processingPolicy,
84 std::optional<size_t> maxParallelRequests,
85 util::TagDecoratorFactory& tagFactory,
86 std::optional<size_t> maxSubscriptionSendQueueSize,
87 OnDisconnectHook onDisconnectHook
88 );
89
91
92 static constexpr std::chrono::milliseconds kCLOSE_CONNECTION_TIMEOUT{500};
93
94 void
95 onGet(std::string const& target, MessageHandler handler);
96
97 void
98 onPost(std::string const& target, MessageHandler handler);
99
100 void
101 onWs(MessageHandler handler);
102
103 void
104 processConnection(ConnectionPtr connection, boost::asio::yield_context yield);
105
106 static void
107 stopConnection(Connection& connection, boost::asio::yield_context yield);
108
109 void
110 stop(boost::asio::yield_context yield);
111
112 bool
113 isStopping() const;
114
115private:
123 bool
124 handleError(Error const& error, Connection const& connection) const;
125
133 bool
134 sequentRequestResponseLoop(
135 Connection& connection,
136 SubscriptionContextPtr& subscriptionContext,
137 boost::asio::yield_context yield
138 );
139
140 bool
141 parallelRequestResponseLoop(
142 Connection& connection,
143 SubscriptionContextPtr& subscriptionContext,
144 boost::asio::yield_context yield
145 );
146
147 std::optional<bool>
148 processRequest(
149 Connection& connection,
150 SubscriptionContextPtr& subscriptionContext,
151 Request const& request,
152 boost::asio::yield_context yield
153 );
154
156 handleRequest(
157 ConnectionMetadata& connectionMetadata,
158 SubscriptionContextPtr& subscriptionContext,
159 Request const& request,
160 boost::asio::yield_context yield
161 );
162};
163
164} // 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:182
Class representing a collection of Prometheus labels.
Definition Label.hpp:59
An interface for a connection metadata class.
Definition Connection.hpp:43
A class representing a connection to a client.
Definition Connection.hpp:100
Represents an HTTP or WebSocket request.
Definition Request.hpp:37
Represents an HTTP or Websocket response.
Definition Response.hpp:40
Definition ConnectionHandler.hpp:52
std::shared_ptr< SubscriptionContextInterface > SubscriptionContextPtr
An alias for shared pointer to a SubscriptionContextInterface.
Definition SubscriptionContextInterface.hpp:86