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