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