Skip to content

Commit 7699574

Browse files
author
knkhmelevsky
committed
feat userver: make ping handler inheritable
commit_hash:86aa4007ea1769c721921c0be19a7dbfcb822db8
1 parent b347492 commit 7699574

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

core/include/userver/server/handlers/ping.hpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,38 @@ namespace server::handlers {
1212

1313
/// @ingroup userver_components userver_http_handlers
1414
///
15-
/// @brief Handler that returns HTTP 200 if the service is OK and able to
16-
/// process requests.
15+
/// @brief Base class for handlers that returns HTTP 200 if the service
16+
/// is OK and able to process requests.
1717
///
1818
/// Uses components::State::IsAnyComponentInFatalState() to detect
1919
/// fatal state (can not process requests).
2020
///
2121
/// ## Static options:
2222
/// Inherits all the options from server::handlers::HttpHandlerBase
2323
/// @ref userver_http_handlers
24+
class PingBase : public HttpHandlerBase {
25+
public:
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+
30+
private:
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
2441
/// and adds the following ones:
2542
///
2643
/// Name | Description | Default value
2744
/// ---- | ----------- | -------------
2845
/// warmup-time-secs | how much time it needs to warmup the server | 0
29-
class Ping final : public HttpHandlerBase {
46+
class Ping final : public PingBase {
3047
public:
3148
Ping(const components::ComponentConfig& config, const components::ComponentContext& component_context);
3249

@@ -43,8 +60,6 @@ class Ping final : public HttpHandlerBase {
4360
private:
4461
void AppendWeightHeaders(http::HttpResponse&) const;
4562

46-
const components::State components_;
47-
4863
std::chrono::steady_clock::time_point load_time_{};
4964
std::chrono::seconds awacs_weight_warmup_time_{60};
5065
};

core/src/server/handlers/ping.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ USERVER_NAMESPACE_BEGIN
1010

1111
namespace server::handlers {
1212

13-
Ping::Ping(const components::ComponentConfig& config, const components::ComponentContext& component_context)
14-
: HttpHandlerBase(config, component_context),
15-
components_(component_context),
16-
awacs_weight_warmup_time_(config["warmup-time-secs"].As<int>(0)) {}
13+
PingBase::PingBase(const components::ComponentConfig& config, const components::ComponentContext& component_context)
14+
: HttpHandlerBase(config, component_context), components_(component_context) {}
1715

18-
std::string Ping::HandleRequestThrow(const http::HttpRequest& request, request::RequestContext& /*context*/) const {
16+
std::string PingBase::HandleRequestThrow(const http::HttpRequest& /*request*/, request::RequestContext& /*context*/)
17+
const {
1918
if (components_.IsAnyComponentInFatalState()) {
2019
throw InternalServerError();
2120
}
@@ -27,6 +26,15 @@ std::string Ping::HandleRequestThrow(const http::HttpRequest& request, request::
2726
throw InternalServerError();
2827
}
2928

29+
return {};
30+
}
31+
32+
Ping::Ping(const components::ComponentConfig& config, const components::ComponentContext& component_context)
33+
: PingBase(config, component_context), awacs_weight_warmup_time_(config["warmup-time-secs"].As<int>(0)) {}
34+
35+
std::string Ping::HandleRequestThrow(const http::HttpRequest& request, request::RequestContext& context) const {
36+
PingBase::HandleRequestThrow(request, context);
37+
3038
auto& response = request.GetHttpResponse();
3139
AppendWeightHeaders(response);
3240

@@ -50,7 +58,7 @@ void Ping::AppendWeightHeaders(http::HttpResponse& response) const {
5058
}
5159

5260
yaml_config::Schema Ping::GetStaticConfigSchema() {
53-
return yaml_config::MergeSchemas<HttpHandlerBase>(R"(
61+
return yaml_config::MergeSchemas<PingBase>(R"(
5462
type: object
5563
description: ping handler config
5664
additionalProperties: false

0 commit comments

Comments
 (0)