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