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