userver: userver/ugrpc/server/service_component_base.hpp Source File
Loading...
Searching...
No Matches
service_component_base.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/server/service_component_base.hpp
4/// @brief @copybrief ugrpc::server::ServiceComponentBase
5
6#include <atomic>
7
8#include <userver/components/component_base.hpp>
9#include <userver/engine/task/task_processor_fwd.hpp>
10#include <userver/middlewares/runner.hpp>
11#include <userver/utils/box.hpp>
12#include <userver/yaml_config/fwd.hpp>
13
14#include <userver/ugrpc/server/middlewares/fwd.hpp>
15#include <userver/ugrpc/server/service_base.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace ugrpc::server {
20
21class ServerComponent;
23class MiddlewareBase;
24struct ServiceInfo;
25
26namespace impl {
27
28/// @brief The interface for a `ServerComponentBase` component. So, `ServerComponentBase` runs with middlewares.
29using MiddlewareRunnerComponentBase = USERVER_NAMESPACE::middlewares::RunnerComponentBase<MiddlewareBase, ServiceInfo>;
30
31} // namespace impl
32
33// clang-format off
34
35/// @ingroup userver_components userver_base_classes
36///
37/// @brief Base class for all the gRPC service components.
38///
39/// ## Static options:
40/// Name | Description | Default value
41/// ---- | ----------- | -------------
42/// task-processor | the task processor to use for responses | taken from grpc-server.service-defaults
43/// disable-user-pipeline-middlewares | flag to disable `groups::User` middlewares from pipeline | false
44/// disable-all-pipeline-middlewares | flag to disable all middlewares from pipeline | false
45/// middlewares | middlewares names to use | `{}` (use server defaults)
46/// status-codes-log-level | gRPC status code string -> span log level map | {}
47
48// clang-format on
49
50class ServiceComponentBase : public impl::MiddlewareRunnerComponentBase {
51public:
52 ServiceComponentBase(const components::ComponentConfig& config, const components::ComponentContext& context);
53
54 ~ServiceComponentBase() override;
55
56 static yaml_config::Schema GetStaticConfigSchema();
57
58protected:
59 /// Derived classes must store the actual service class in a field and call
60 /// RegisterService with it
62
63 /// @overload
65
66private:
67 ServerComponent& server_;
68 ServiceConfig config_;
69 std::atomic<bool> registered_{false};
70 utils::Box<ServiceInfo> info_;
71};
72
73namespace impl {
74
75template <typename ServiceInterface>
76// NOLINTNEXTLINE(fuchsia-multiple-inheritance)
77class ServiceComponentBase : public server::ServiceComponentBase, public ServiceInterface {
78 static_assert(std::is_base_of_v<ServiceBase, ServiceInterface> || std::is_base_of_v<GenericServiceBase, ServiceInterface>);
79
80public:
81 ServiceComponentBase(const components::ComponentConfig& config, const components::ComponentContext& context)
82 : server::ServiceComponentBase(config, context), ServiceInterface() {
83 // At this point the derived class that implements ServiceInterface is not
84 // constructed yet. We rely on the implementation detail that the methods of
85 // ServiceInterface are never called right after RegisterService. Unless
86 // Server starts during the construction of this component (which is an
87 // error anyway), we should be fine.
88 RegisterService(*this);
89 }
90
91private:
92 using server::ServiceComponentBase::RegisterService;
93};
94
95} // namespace impl
96
97} // namespace ugrpc::server
98
99USERVER_NAMESPACE_END