userver: userver/server/handlers/ping.hpp Source File
Loading...
Searching...
No Matches
ping.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/ping.hpp
4/// @brief @copybrief server::handlers::Ping
5
6#include <userver/components/state.hpp>
7#include <userver/server/handlers/http_handler_base.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace server::handlers {
12
13/// @ingroup userver_components userver_http_handlers
14///
15/// @brief Base class for handlers that returns HTTP 200 if the service
16/// is OK and able to process requests.
17///
18/// Uses components::State::IsAnyComponentInFatalState() to detect
19/// fatal state (can not process requests).
20///
21/// ## Static options:
22/// Inherits all the options from server::handlers::HttpHandlerBase
23/// @ref userver_http_handlers
24class PingBase : public HttpHandlerBase {
25public:
26 PingBase(const components::ComponentConfig& config, const components::ComponentContext& component_context);
27
28 std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext& context) const override;
29
30private:
31 const components::State components_;
32};
33
34/// @ingroup userver_components userver_http_handlers
35///
36/// @brief Ping handler implementation with warmup
37///
38/// ## Static options:
39/// Inherits all the options from server::handlers::PingBase
40/// @ref userver_http_handlers
41/// and adds the following ones:
42///
43/// Name | Description | Default value
44/// ---- | ----------- | -------------
45/// warmup-time-secs | how much time it needs to warmup the server | 0
46class Ping final : public PingBase {
47public:
48 Ping(const components::ComponentConfig& config, const components::ComponentContext& component_context);
49
50 /// @ingroup userver_component_names
51 /// @brief The default name of server::handlers::Ping
52 static constexpr std::string_view kName = "handler-ping";
53
54 std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext& context) const override;
55
56 void OnAllComponentsLoaded() override;
57
58 static yaml_config::Schema GetStaticConfigSchema();
59
60private:
61 void AppendWeightHeaders(http::HttpResponse&) const;
62
63 std::chrono::steady_clock::time_point load_time_{};
64 std::chrono::seconds awacs_weight_warmup_time_{60};
65};
66
67} // namespace server::handlers
68
69template <>
70inline constexpr bool components::kHasValidate<server::handlers::Ping> = true;
71
72USERVER_NAMESPACE_END