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/yaml_config/fwd.hpp>
12
13#include <userver/ugrpc/server/middlewares/fwd.hpp>
14#include <userver/ugrpc/server/service_base.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace ugrpc::server {
19
20class ServerComponent;
22class MiddlewareBase;
23struct ServiceInfo;
24
25namespace impl {
26
27/// @brief The interface for a `ServerComponentBase` component. So, `ServerComponentBase` runs with middlewares.
28using MiddlewareRunnerComponentBase = USERVER_NAMESPACE::middlewares::RunnerComponentBase<MiddlewareBase, ServiceInfo>;
29
30} // namespace impl
31
32/// @ingroup userver_components userver_base_classes
33///
34/// @brief Base class for all the gRPC service components.
35///
36/// ## Static options of ugrpc::server::ServiceComponentBase :
37/// @include{doc} scripts/docs/en/components_schema/grpc/src/ugrpc/server/service_component_base.md
38///
39/// Options inherited from @ref middlewares::RunnerComponentBase :
40/// @include{doc} scripts/docs/en/components_schema/core/src/middlewares/runner_component_base.md
41///
42/// Options inherited from @ref components::ComponentBase :
43/// @include{doc} scripts/docs/en/components_schema/core/src/components/impl/component_base.md
44class ServiceComponentBase : public impl::MiddlewareRunnerComponentBase {
45public:
46 ServiceComponentBase(const components::ComponentConfig& config, const components::ComponentContext& context);
47
48 ~ServiceComponentBase() override;
49
50 static yaml_config::Schema GetStaticConfigSchema();
51
52protected:
53 /// Derived classes must store the actual service class in a field and call
54 /// RegisterService with it
56
57 /// @overload
59
60private:
61 ServerComponent& server_;
62 ServiceConfig config_;
63 std::string component_name_;
64 std::atomic<bool> registered_{false};
65};
66
67namespace impl {
68
69template <typename ServiceInterface>
70// NOLINTNEXTLINE(fuchsia-multiple-inheritance)
71class ServiceComponentBase : public server::ServiceComponentBase, public ServiceInterface {
72 static_assert(std::is_base_of_v<ServiceBase, ServiceInterface> || std::is_base_of_v<GenericServiceBase, ServiceInterface>);
73
74public:
75 ServiceComponentBase(const components::ComponentConfig& config, const components::ComponentContext& context)
76 : server::ServiceComponentBase(config, context),
77 ServiceInterface()
78 {
79 // At this point the derived class that implements ServiceInterface is not
80 // constructed yet. We rely on the implementation detail that the methods of
81 // ServiceInterface are never called right after RegisterService. Unless
82 // Server starts during the construction of this component (which is an
83 // error anyway), we should be fine.
84 RegisterService(*this);
85 }
86
87private:
88 using server::ServiceComponentBase::RegisterService;
89};
90
91} // namespace impl
92
93} // namespace ugrpc::server
94
95USERVER_NAMESPACE_END