userver: userver/ugrpc/server/service_component_base.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 MiddlewareRunner = 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 pipline | false
45/// middlewares | middlewares names to use | `{}` (use server defaults)
46
47// clang-format on
48
49class ServiceComponentBase : public impl::MiddlewareRunner {
50public:
51 ServiceComponentBase(const components::ComponentConfig& config, const components::ComponentContext& context);
52
53 ~ServiceComponentBase() override;
54
55 static yaml_config::Schema GetStaticConfigSchema();
56
57protected:
58 /// Derived classes must store the actual service class in a field and call
59 /// RegisterService with it
61
62 /// @overload
64
65private:
66 ServerComponent& server_;
67 ServiceConfig config_;
68 std::atomic<bool> registered_{false};
69 utils::Box<ServiceInfo> info_;
70};
71
72namespace impl {
73
74template <typename ServiceInterface>
75// NOLINTNEXTLINE(fuchsia-multiple-inheritance)
76class ServiceComponentBase : public server::ServiceComponentBase, public ServiceInterface {
77 static_assert(std::is_base_of_v<ServiceBase, ServiceInterface> || std::is_base_of_v<GenericServiceBase, ServiceInterface>);
78
79public:
80 ServiceComponentBase(const components::ComponentConfig& config, const components::ComponentContext& context)
81 : server::ServiceComponentBase(config, context), ServiceInterface() {
82 // At this point the derived class that implements ServiceInterface is not
83 // constructed yet. We rely on the implementation detail that the methods of
84 // ServiceInterface are never called right after RegisterService. Unless
85 // Server starts during the construction of this component (which is an
86 // error anyway), we should be fine.
87 RegisterService(*this);
88 }
89
90private:
91 using server::ServiceComponentBase::RegisterService;
92};
93
94} // namespace impl
95
96} // namespace ugrpc::server
97
98USERVER_NAMESPACE_END