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
11#include <userver/ugrpc/server/middlewares/fwd.hpp>
12#include <userver/ugrpc/server/service_base.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace ugrpc::server {
17
18class ServerComponent;
20
21// clang-format off
22
23/// @ingroup userver_components userver_base_classes
24///
25/// @brief Base class for all the gRPC service components.
26///
27/// ## Static options:
28/// Name | Description | Default value
29/// ---- | ----------- | -------------
30/// task-processor | the task processor to use for responses | taken from grpc-server.service-defaults
31/// middlewares | middleware component names to use for each RPC call, can be empty array ([]) | taken from grpc-server.service-defaults
32
33// clang-format on
34
36public:
37 ServiceComponentBase(const components::ComponentConfig& config, const components::ComponentContext& context);
38
39 static yaml_config::Schema GetStaticConfigSchema();
40
41protected:
42 /// Derived classes must store the actual service class in a field and call
43 /// RegisterService with it
45
46 /// @overload
48
49private:
50 ServerComponent& server_;
51 ServiceConfig config_;
52 std::atomic<bool> registered_{false};
53};
54
55namespace impl {
56
57template <typename ServiceInterface>
58// NOLINTNEXTLINE(fuchsia-multiple-inheritance)
59class ServiceComponentBase : public server::ServiceComponentBase, public ServiceInterface {
60 static_assert(std::is_base_of_v<ServiceBase, ServiceInterface> || std::is_base_of_v<GenericServiceBase, ServiceInterface>);
61
62public:
63 ServiceComponentBase(const components::ComponentConfig& config, const components::ComponentContext& context)
64 : server::ServiceComponentBase(config, context), ServiceInterface() {
65 // At this point the derived class that implements ServiceInterface is not
66 // constructed yet. We rely on the implementation detail that the methods of
67 // ServiceInterface are never called right after RegisterService. Unless
68 // Server starts during the construction of this component (which is an
69 // error anyway), we should be fine.
70 RegisterService(*this);
71 }
72
73private:
74 using server::ServiceComponentBase::RegisterService;
75};
76
77} // namespace impl
78
79} // namespace ugrpc::server
80
81USERVER_NAMESPACE_END