userver: userver/server/component.hpp Source File
⚠️ This is the documentation for an old userver version. Click here to switch to the latest version.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
component.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/component.hpp
4/// @brief @copybrief components::Server
5
6#include <memory>
7
8#include <userver/components/loggable_component_base.hpp>
9#include <userver/engine/task/task_processor_fwd.hpp>
10#include <userver/server/server.hpp>
11#include <userver/utils/statistics/entry.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace components {
16
17// clang-format off
18
19/// @ingroup userver_components
20///
21/// @brief Component that listens for incoming requests, manages
22/// incoming connections and passes the requests to the appropriate handler.
23///
24/// Starts listening and accepting connections only after **all** the
25/// components are loaded.
26///
27/// All the classes inherited from server::handlers::HttpHandlerBase and
28/// registered in components list bind to the components::Server component.
29///
30/// ## Dynamic config
31/// * @ref USERVER_LOG_REQUEST
32/// * @ref USERVER_LOG_REQUEST_HEADERS
33/// * @ref USERVER_DEADLINE_PROPAGATION_ENABLED
34/// * @ref USERVER_CANCEL_HANDLE_REQUEST_BY_DEADLINE
35///
36/// ## Static options:
37/// Name | Description | Default value
38/// ---- | ----------- | -------------
39/// logger_access | set to logger name from components::Logging component to write access logs into it; do not set to avoid writing access logs | -
40/// logger_access_tskv | set to logger name from components::Logging component to write access logs in TSKV format into it; do not set to avoid writing access logs | -
41/// max_response_size_in_flight | set it to the size of response in bytes and the component will drop bigger responses from handlers that allow throttling | -
42/// server-name | value to send in HTTP Server header | value from utils::GetUserverIdentifier()
43/// listener | (*required*) *see below* | -
44/// listener-monitor | *see below* | -
45/// set-response-server-hostname | set to true to add the `X-YaTaxi-Server-Hostname` header with instance name, set to false to not add the header | false
46///
47/// Server is configured by 'listener' and 'listener-monitor' entries.
48/// 'listener' is a required entry that describes the request processing
49/// socket. 'listener-monitor' is an optional entry that describes the special
50/// monitoring socket, used for getting statistics and processing utility
51/// requests that should succeed even is the main socket is under heavy
52/// pressure.
53///
54/// Each of the 'listener' and 'listener-monitor' may be configured with the
55/// following options:
56///
57/// Name | Description | Default value
58/// ---- | ----------- | -------------
59/// address | IPv6 or IPv4 network interface to bind to | '::' (all the IPv6 and IPv4 local interfaces)
60/// port | port to listen on | 0
61/// unix-socket | unix socket to listen on instead of listening on a port and network address | ''
62/// max_connections | max connections count to keep | 32768
63/// task_processor | task processor to process incoming requests | -
64/// backlog | max count of new connections pending acceptance | 1024
65/// tls.ca | paths to TLS CAs for client authentication | -
66/// tls.cert | path to TLS server certificate | -
67/// tls.private-key | path to TLS server certificate private key | -
68/// tls.private-key-passphrase-name | passphrase name located in secdist's "passphrases" section | -
69/// handler-defaults.max_url_size | max path/URL size or empty to not limit | 8192
70/// handler-defaults.max_request_size | max size of the whole request | 1024 * 1024
71/// handler-defaults.max_headers_size | max request headers size | 65536
72/// handler-defaults.parse_args_from_body | optional field to parse request according to x-www-form-urlencoded rules and make parameters accessible as query parameters | false
73/// handler-defaults.set_tracing_headers | whether to set http tracing headers (X-YaTraceId, X-YaSpanId, X-RequestId) | true
74/// handler-defaults.deadline_propagation_enabled | when `false`, disables HTTP handler deadline propagation | true
75/// handler-defaults.deadline_expired_status_code | the HTTP status code to return if the request deadline expires | 498
76/// connection.in_buffer_size | size of the buffer to preallocate for request receive: bigger values use more RAM and less CPU | 32 * 1024
77/// connection.requests_queue_size_threshold | drop requests from handlers that allow throttling if there's more pending requests than allowed by this value | 100
78/// connection.keepalive_timeout | timeout in seconds to drop connection if there's not data received from it | 600
79/// connection.stream_close_check_delay | delay in microseconds of the start of stream close check routine; do not set if not sure what it is doing | 20ms
80/// shards | how many concurrent tasks harvest data from a single socket; do not set if not sure what it is doing | -
81/// middleware-pipeline-builder | name of a component to build a server-wide middleware pipeline | default-server-middleware-pipeline-builder
82///
83/// @see @ref scripts/docs/en/userver/http_server.md
84
85// clang-format on
86
87class Server final : public LoggableComponentBase {
88 public:
89 /// @ingroup userver_component_names
90 /// @brief The default name of components::Server component
91 static constexpr std::string_view kName = "server";
92
93 Server(const components::ComponentConfig& component_config,
94 const components::ComponentContext& component_context);
95
96 ~Server() override;
97
98 void OnAllComponentsLoaded() override;
99
100 void OnAllComponentsAreStopping() override;
101
102 const server::Server& GetServer() const;
103
104 server::Server& GetServer();
105
106 void AddHandler(const server::handlers::HttpHandlerBase& handler,
107 engine::TaskProcessor& task_processor);
108
109 static yaml_config::Schema GetStaticConfigSchema();
110
111 private:
112 void WriteStatistics(utils::statistics::Writer& writer);
113
114 std::unique_ptr<server::Server> server_;
115 utils::statistics::Entry server_statistics_holder_;
116 utils::statistics::Entry handler_statistics_holder_;
117};
118
119template <>
120inline constexpr bool kHasValidate<Server> = true;
121
122} // namespace components
123
124USERVER_NAMESPACE_END