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