userver: userver/ugrpc/server/server.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
server.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/server/server.hpp
4/// @brief @copybrief ugrpc::server::Server
5
6#include <functional>
7#include <memory>
8#include <unordered_map>
9
10#include <grpcpp/completion_queue.h>
11#include <grpcpp/server_builder.h>
12
13#include <userver/dynamic_config/source.hpp>
14#include <userver/engine/task/task_processor_fwd.hpp>
15#include <userver/logging/level.hpp>
16#include <userver/server/congestion_control/sensor.hpp>
17#include <userver/utils/function_ref.hpp>
18#include <userver/utils/impl/internal_tag.hpp>
19#include <userver/utils/statistics/fwd.hpp>
20#include <userver/yaml_config/fwd.hpp>
21
22#include <userver/ugrpc/impl/statistics.hpp>
23#include <userver/ugrpc/server/middlewares/fwd.hpp>
24#include <userver/ugrpc/server/service_base.hpp>
25
26USERVER_NAMESPACE_BEGIN
27
28namespace ugrpc::impl {
29class CompletionQueuePoolBase;
30} // namespace ugrpc::impl
31
32namespace ugrpc::server {
33
35
36struct TlsConfig {
37 // Certificate Authority, for client auth
38 std::optional<std::string> ca;
39
40 // Server certificate private key
41 std::optional<std::string> key;
42
43 // Server certificate
44 std::optional<std::string> cert;
45};
46
47/// Settings relating to the whole gRPC server
48struct ServerConfig final {
49 /// The port to listen to. If `0`, a free port will be picked automatically.
50 /// If none, the ports have to be configured programmatically using
51 /// Server::WithServerBuilder.
52 std::optional<int> port{0};
53
54 /// Absolute path to the unix socket to listen to.
55 /// A server can listen to both port and unix socket simultaneously.
56 std::optional<std::string> unix_socket_path{std::nullopt};
57
58 /// Number of completion queues to create. Should be ~2 times less than number
59 /// of worker threads for best RPS.
60 std::size_t completion_queue_num{2};
61
62 /// Optional grpc-core channel args
63 /// @see https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
64 std::unordered_map<std::string, std::string> channel_args{};
65
66 /// The logging level override for the internal grpcpp library. Must be either
67 /// `kDebug`, `kInfo` or `kError`.
69
70 /// Serve a web page with runtime info about gRPC connections
71 bool enable_channelz{false};
72
73 /// TLS settings
75};
76
77/// @brief Manages the gRPC server
78///
79/// All methods are thread-safe.
80/// Usually retrieved from ugrpc::server::ServerComponent.
81class Server final : public USERVER_NAMESPACE::server::congestion_control::RequestsSource {
82public:
83 using SetupHook = utils::function_ref<void(grpc::ServerBuilder&)>;
84
85 /// @brief Start building the server
86 explicit Server(
87 ServerConfig&& config,
88 utils::statistics::Storage& statistics_storage,
89 dynamic_config::Source config_source
90 );
91
92 Server(Server&&) = delete;
93 Server& operator=(Server&&) = delete;
94 ~Server() override;
95
96 /// @brief Register a service implementation in the server. The user or the
97 /// component is responsible for keeping `service` and `middlewares` alive at
98 /// least until `Stop` is called.
99 void AddService(ServiceBase& service, ServiceConfig&& config);
100
101 /// @overload
102 void AddService(GenericServiceBase& service, ServiceConfig&& config);
103
104 /// @brief Get names of all registered services
105 std::vector<std::string_view> GetServiceNames() const;
106
107 /// @brief For advanced configuration of the gRPC server
108 /// @note The ServerBuilder must not be stored and used outside of `setup`.
109 void WithServerBuilder(SetupHook setup);
110
111 /// @brief Start accepting requests
112 /// @note Must be called at most once after all the services are registered
113 void Start();
114
115 /// @returns The port assigned using `AddListeningPort`
116 /// @note Only available after 'Start' has returned
117 int GetPort() const noexcept;
118
119 /// @brief Stop accepting requests. Also destroys server statistics and closes
120 /// the associated CompletionQueue.
121 /// @note Should be called at least once before the services are destroyed
122 void Stop() noexcept;
123
124 /// Same as Stop, but:
125 /// - does not destroy server statistics
126 /// - does not close the associated CompletionQueue
127 /// Stop must still be called. StopServing is also useful for testing.
128 void StopServing() noexcept;
129
130 /// @cond
131 // For internal use only.
132 std::uint64_t GetTotalRequests() const override;
133
134 // For internal use only.
135 ugrpc::impl::CompletionQueuePoolBase& GetCompletionQueues(utils::impl::InternalTag);
136 /// @endcond
137
138private:
139 class Impl;
140 std::unique_ptr<Impl> impl_;
141};
142
143} // namespace ugrpc::server
144
145USERVER_NAMESPACE_END