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///
52/// ## Static configuration example:
53///
54/// @snippet components/common_component_list_test.cpp Sample http client component config
55
56// clang-format on
57class HttpClient final : public LoggableComponentBase {
58 public:
59 /// @ingroup userver_component_names
60 /// @brief The default name of components::HttpClient component
61 static constexpr std::string_view kName = "http-client";
62
63 HttpClient(const ComponentConfig&, const ComponentContext&);
64
65 ~HttpClient() override;
66
67 clients::http::Client& GetHttpClient();
68
69 static yaml_config::Schema GetStaticConfigSchema();
70
71 private:
72 void OnConfigUpdate(const dynamic_config::Snapshot& config);
73
74 void WriteStatistics(utils::statistics::Writer& writer);
75
76 static std::vector<utils::NotNull<clients::http::Plugin*>> FindPlugins(
77 const std::vector<std::string>& names,
78 const components::ComponentContext& context);
79
80 const bool disable_pool_stats_;
81 clients::http::Client http_client_;
82 concurrent::AsyncEventSubscriberScope subscriber_scope_;
83 utils::statistics::Entry statistics_holder_;
84};
85
86template <>
87inline constexpr bool kHasValidate<HttpClient> = true;
88
89} // namespace components
90
91USERVER_NAMESPACE_END