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