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 /// Whether to apply OpenTelemetry trace-sampling to incoming gRPC requests.
82};
83
84/// @brief Manages the gRPC server
85///
86/// All methods are thread-safe.
87/// Usually retrieved from ugrpc::server::ServerComponent.
88class Server final : public USERVER_NAMESPACE::server::congestion_control::RequestsSource {
89public:
90 using SetupHook = utils::function_ref<void(grpc::ServerBuilder&)>;
91
92 /// @brief Start building the server
93 explicit Server(
94 utils::ResourceScopeStorage& scope_storage,
95 ServerConfig&& config,
96 utils::statistics::Storage& statistics_storage,
97 dynamic_config::Source config_source
98 );
99
100 Server(Server&&) = delete;
101 Server& operator=(Server&&) = delete;
102 ~Server() override;
103
104 /// @brief Register a service implementation in the server. The user or the
105 /// component is responsible for keeping `service` and `middlewares` alive at
106 /// least until `Stop` is called.
107 void AddService(ServiceBase& service, ServiceConfig&& config);
108
109 /// @overload
110 void AddService(GenericServiceBase& service, ServiceConfig&& config);
111
112 /// @brief Get names of all registered services
113 std::vector<std::string_view> GetServiceNames() const;
114
115 /// @brief For advanced configuration of the gRPC server
116 /// @note The ServerBuilder must not be stored and used outside of `setup`.
117 void WithServerBuilder(SetupHook setup);
118
119 /// @brief Start accepting requests
120 /// @note Must be called at most once after all the services are registered
121 void Start();
122
123 /// @returns The port assigned using `AddListeningPort`
124 /// @note Only available after 'Start' has returned
125 int GetPort() const noexcept;
126
127 /// @brief Stop accepting requests. Also destroys server statistics and closes
128 /// the associated CompletionQueue.
129 /// @note Should be called at least once before the services are destroyed
130 void Stop() noexcept;
131
132 /// Same as Stop, but:
133 /// - does not destroy server statistics
134 /// - does not close the associated CompletionQueue
135 /// Stop must still be called. StopServing is also useful for testing.
136 void StopServing(std::optional<engine::Deadline> serving_shutdown_deadline = std::nullopt) noexcept;
137
138 /// @cond
139 // For internal use only.
140 std::uint64_t GetTotalRequests() const override;
141
142 // For internal use only.
143 ugrpc::impl::CompletionQueuePoolBase& GetCompletionQueues(utils::impl::InternalTag);
144 /// @endcond
145
146private:
147 class Impl;
148 std::unique_ptr<Impl> impl_;
149};
150
151} // namespace ugrpc::server
152
153USERVER_NAMESPACE_END