userver: userver/clients/http/client_core.hpp Source File
Loading...
Searching...
No Matches
client_core.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/clients/http/client_core.hpp
4/// @brief @copybrief clients::http::ClientCore
5
6#ifdef USERVER_TVM2_HTTP_CLIENT
7#error Use clients::Http from clients/http.hpp instead
8#endif
9
10#include <memory>
11
12#include <userver/moodycamel/concurrentqueue_fwd.h>
13
14#include <userver/clients/dns/resolver_fwd.hpp>
15#include <userver/clients/http/client.hpp>
16#include <userver/clients/http/config.hpp>
17#include <userver/clients/http/request.hpp>
18#include <userver/engine/task/task_processor_fwd.hpp>
19#include <userver/rcu/rcu.hpp>
20#include <userver/utils/fast_pimpl.hpp>
21#include <userver/utils/impl/internal_tag_fwd.hpp>
22#include <userver/utils/not_null.hpp>
23#include <userver/utils/periodic_task.hpp>
24#include <userver/utils/statistics/fwd.hpp>
25#include <userver/utils/swappingsmart.hpp>
26#include <userver/yaml_config/fwd.hpp>
27
28USERVER_NAMESPACE_BEGIN
29
30namespace tracing {
32} // namespace tracing
33
34namespace curl {
35class easy;
36class multi;
37class ConnectRateLimiter;
38} // namespace curl
39
40namespace engine::ev {
41class ThreadPool;
42} // namespace engine::ev
43
44namespace clients::http {
45namespace impl {
46class EasyWrapper;
47} // namespace impl
48
49struct TestsuiteConfig;
50class Statistics;
51struct PoolStatistics;
52struct InstanceStatistics;
53class DestinationStatistics;
54
55/// @ingroup userver_clients
56///
57/// @brief HTTP client that returns a HTTP request builder from
58/// CreateRequest().
59///
60/// Usually retrieved from @ref components::HttpClientCore component.
61///
62/// ## Example usage:
63///
64/// @snippet clients/http/client_test.cpp Sample HTTP Client usage
65class ClientCore final : public Client {
66public:
67 /// @cond
68 // For internal use only
69 ClientCore(utils::impl::InternalTag, ClientSettings settings, engine::TaskProcessor& fs_task_processor);
70 /// @endcond
71
72 ~ClientCore() override;
73
74 /// @brief Returns a HTTP request builder type with preset values of
75 /// User-Agent and some of the Testsuite stuff (if any).
76 ///
77 /// @note This method does not apply middlewares, they could be applied
78 /// manually using @ref clients::http::Request::SetMiddlewaresList()
79 /// @note This method is thread-safe despite being non-const.
80 Request CreateRequest() override;
81
82 /// @cond
83 // For internal use only.
84 void SetMultiplexingEnabled(bool enabled);
85
86 // For internal use only.
87 void SetMaxHostConnections(size_t max_host_connections);
88
89 // For internal use only.
90 PoolStatistics GetPoolStatistics() const;
91
92 // Set max number of automatically created destination metrics.
93 // For internal use only.
94 void SetDestinationMetricsAutoMaxSize(size_t max_size);
95
96 // For internal use only.
97 const http::DestinationStatistics& GetDestinationStatistics() const;
98
99 // For internal use only.
100 void SetTestsuiteConfig(TestsuiteConfig&& config);
101
102 // For internal use only.
103 void SetAllowedUrlsExtra(std::vector<std::string>&& urls);
104
105 // For internal use only.
106 void SetConfig(const impl::Config&);
107
108 // For internal use only.
109 void ResetUserAgent(std::optional<std::string> user_agent = std::nullopt);
110
111 // For internal use only.
112 void SetDnsResolver(clients::dns::Resolver* resolver);
113
114 // For internal use only.
115 std::size_t GetActiveRequestCountDebug() const;
116 /// @endcond
117
118private:
119 void ReinitEasy();
120
121 InstanceStatistics GetMultiStatistics(size_t n) const;
122
123 size_t FindMultiIndex(const curl::multi*) const;
124
125 // Functions for EasyWrapper that must be noexcept, as they are called from
126 // the EasyWrapper destructor.
127 friend class impl::EasyWrapper;
128 void IncPending() noexcept { ++pending_tasks_; }
129 void DecPending() noexcept { --pending_tasks_; }
130 void PushIdleEasy(std::shared_ptr<curl::easy>&& easy) noexcept;
131
132 std::shared_ptr<curl::easy> TryDequeueIdle() noexcept;
133
134 std::atomic<std::size_t> pending_tasks_{0};
135
136 const DeadlinePropagationConfig deadline_propagation_config_;
137 CancellationPolicy cancellation_policy_;
138
139 std::shared_ptr<DestinationStatistics> destination_statistics_;
140 std::unique_ptr<engine::ev::ThreadPool> thread_pool_;
141 std::vector<Statistics> statistics_;
142 std::vector<std::unique_ptr<curl::multi>> multis_;
143
144 static constexpr size_t kIdleQueueSize = 616;
145 static constexpr size_t kIdleQueueAlignment = 8;
146 using IdleQueueTraits = moodycamel::ConcurrentQueueDefaultTraits;
147 using IdleQueueValue = std::shared_ptr<curl::easy>;
148 using IdleQueue = moodycamel::ConcurrentQueue<IdleQueueValue, IdleQueueTraits>;
149 utils::FastPimpl<IdleQueue, kIdleQueueSize, kIdleQueueAlignment> idle_queue_;
150
151 engine::TaskProcessor& fs_task_processor_;
152 std::optional<std::string> user_agent_;
153
154 utils::SwappingSmart<const curl::easy> easy_;
155 utils::PeriodicTask easy_reinit_task_;
156
157 // Testsuite support
158 std::shared_ptr<const TestsuiteConfig> testsuite_config_;
159 rcu::Variable<std::vector<std::string>> allowed_urls_extra_;
160
161 std::shared_ptr<curl::ConnectRateLimiter> connect_rate_limiter_;
162
163 clients::dns::Resolver* resolver_{nullptr};
164 utils::NotNull<const tracing::TracingManagerBase*> tracing_manager_;
165};
166
167void DumpMetric(utils::statistics::Writer& writer, const DestinationStatistics& stats);
168
169} // namespace clients::http
170
171USERVER_NAMESPACE_END