userver: userver/clients/http/component.hpp Source File
Loading...
Searching...
No Matches
component.hpp
Go to the documentation of this file.
1#pragma once
2
3#ifdef USERVER_TVM2_HTTP_CLIENT
4#error Use components::Http from clients/http.hpp instead
5#endif
6
7/// @file userver/clients/http/component.hpp
8/// @brief @copybrief components::HttpClient
9
10#include <userver/clients/http/client.hpp>
11#include <userver/components/loggable_component_base.hpp>
12#include <userver/concurrent/async_event_source.hpp>
13#include <userver/dynamic_config/snapshot.hpp>
14#include <userver/utils/statistics/entry.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace components {
19
20// clang-format off
21
22/// @ingroup userver_components
23///
24/// @brief Component that manages clients::http::Client.
25///
26/// Returned references to clients::http::Client live for a lifetime of the
27/// component and are safe for concurrent use.
28///
29/// ## Dynamic options:
30/// * @ref HTTP_CLIENT_CONNECT_THROTTLE
31/// * @ref HTTP_CLIENT_CONNECTION_POOL_SIZE
32/// * @ref USERVER_HTTP_PROXY
33///
34/// ## Static options:
35/// Name | Description | Default value
36/// ---- | ----------- | -------------
37/// pool-statistics-disable | set to true to disable statistics for connection pool | false
38/// thread-name-prefix | set OS thread name to this value | ''
39/// threads | number of threads to process low level HTTP related IO system calls | 8
40/// defer-events | whether to defer events execution to a periodic timer; might affect timings a bit, might boost performance, use with care | false
41/// fs-task-processor | task processor to run blocking HTTP related calls, like DNS resolving or hosts reading | -
42/// destination-metrics-auto-max-size | set max number of automatically created destination metrics | 100
43/// user-agent | User-Agent HTTP header to show on all requests, result of utils::GetUserverIdentifier() if empty | empty
44/// bootstrap-http-proxy | HTTP proxy to use at service start. Will be overridden by @ref USERVER_HTTP_PROXY at runtime config update | ''
45/// testsuite-enabled | enable testsuite testing support | false
46/// testsuite-timeout | if set, force the request timeout regardless of the value passed in code | -
47/// testsuite-allowed-url-prefixes | if set, checks that all URLs start with any of the passed prefixes, asserts if not. Set for testing purposes only. | ''
48/// dns_resolver | server hostname resolver type (getaddrinfo or async) | 'async'
49/// set-deadline-propagation-header | whether to set http::common::kXYaTaxiClientTimeoutMs request header, see @ref scripts/docs/en/userver/deadline_propagation.md | true
50/// plugins | Plugin names to apply. A plugin component is called "http-client-plugin-" plus the plugin name. | []
51/// cancellation-policy | Cancellation policy for new requests. | cancel
52///
53/// ## Static configuration example:
54///
55/// @snippet components/common_component_list_test.cpp Sample http client component config
56
57// clang-format on
58class HttpClient final : public LoggableComponentBase {
59 public:
60 /// @ingroup userver_component_names
61 /// @brief The default name of components::HttpClient component
62 static constexpr std::string_view kName = "http-client";
63
64 HttpClient(const ComponentConfig&, const ComponentContext&);
65
66 ~HttpClient() override;
67
68 clients::http::Client& GetHttpClient();
69
70 static yaml_config::Schema GetStaticConfigSchema();
71
72 private:
73 void OnConfigUpdate(const dynamic_config::Snapshot& config);
74
75 void WriteStatistics(utils::statistics::Writer& writer);
76
77 static std::vector<utils::NotNull<clients::http::Plugin*>> FindPlugins(
78 const std::vector<std::string>& names,
79 const components::ComponentContext& context);
80
81 const bool disable_pool_stats_;
82 clients::http::Client http_client_;
83 concurrent::AsyncEventSubscriberScope subscriber_scope_;
84 utils::statistics::Entry statistics_holder_;
85};
86
87template <>
88inline constexpr bool kHasValidate<HttpClient> = true;
89
90} // namespace components
91
92USERVER_NAMESPACE_END