userver: userver/ugrpc/client/client_factory.hpp Source File
Loading...
Searching...
No Matches
client_factory.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/client/client_factory.hpp
4/// @brief @copybrief ugrpc::client::ClientFactory
5
6#include <cstddef>
7#include <memory>
8#include <string>
9#include <unordered_map>
10#include <utility>
11
12#include <grpcpp/completion_queue.h>
13#include <grpcpp/security/credentials.h>
14#include <grpcpp/support/channel_arguments.h>
15
16#include <userver/dynamic_config/source.hpp>
17#include <userver/engine/task/task_processor_fwd.hpp>
18#include <userver/logging/level.hpp>
19#include <userver/storages/secdist/secdist.hpp>
20#include <userver/testsuite/grpc_control.hpp>
21
22#include <userver/ugrpc/client/fwd.hpp>
23#include <userver/ugrpc/client/impl/channel_cache.hpp>
24#include <userver/ugrpc/client/impl/client_data.hpp>
25#include <userver/ugrpc/client/middlewares/base.hpp>
26
27USERVER_NAMESPACE_BEGIN
28
29namespace ugrpc::impl {
30class StatisticsStorage;
31class CompletionQueuePoolBase;
32} // namespace ugrpc::impl
33
34namespace ugrpc::client {
35
36/// Settings relating to the ClientFactory
37struct ClientFactorySettings final {
38 /// gRPC channel credentials, none by default
39 std::shared_ptr<grpc::ChannelCredentials> credentials{
40 grpc::InsecureChannelCredentials()};
41
42 /// gRPC channel credentials by client_name. If not set, default `credentials`
43 /// is used instead.
46
47 /// Optional grpc-core channel args
48 /// @see https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
49 grpc::ChannelArguments channel_args{};
50
51 /// The logging level override for the internal grpcpp library. Must be either
52 /// `kDebug`, `kInfo` or `kError`.
54
55 /// Number of underlying channels that will be created for every client
56 /// in this factory.
57 std::size_t channel_count{1};
58};
59
60/// Settings relating to creation of a code-generated client
61struct ClientSettings final {
62 /// **(Required)**
63 /// The name of the client, for diagnostics, credentials and middlewares.
64 std::string client_name;
65
66 /// **(Required)**
67 /// The URI to connect to, e.g. `http://my.domain.com:8080`.
68 /// Should not include any HTTP path, just schema, domain name and port. Unix
69 /// sockets are also supported. For details, see:
70 /// https://grpc.github.io/grpc/cpp/md_doc_naming.html
71 std::string endpoint;
72
73 /// **(Optional)**
74 /// The name of the QOS
75 /// @ref scripts/docs/en/userver/dynamic_config.md "dynamic config"
76 /// that will be applied automatically to every RPC.
77 ///
78 /// Timeout from QOS config is ignored if:
79 ///
80 /// * an explicit `qos` parameter is specified at RPC creation, or
81 /// * deadline is specified in the `client_context` passed at RPC creation.
82 ///
83 /// ## Client QOS config definition sample
84 ///
85 /// @snippet grpc/tests/tests/unit_test_client_qos.hpp qos config key
86 const dynamic_config::Key<ClientQos>* client_qos{nullptr};
87};
88
89/// @ingroup userver_clients
90///
91/// @brief Creates gRPC clients.
92///
93/// Typically obtained from ugrpc::client::ClientFactoryComponent.
94/// In tests and benchmarks, obtained from ugrpc::tests::ServiceBase and
95/// friends.
96///
97/// Has a minimal built-in channel cache:
98/// as long as a channel to the same endpoint is used somewhere, the same
99/// channel is given out.
100class ClientFactory final {
101 public:
102 /// @brief Make a client of the specified code-generated type.
103 template <typename Client>
104 Client MakeClient(ClientSettings&& settings);
105
106 /// @deprecated Use the overload taking @ref ClientSettings instead.
107 /// @brief Make a client of the specified code-generated type.
108 /// @param client_name see @ref ClientSettings
109 /// @param endpoint see @ref ClientSettings
110 template <typename Client>
111 Client MakeClient(const std::string& client_name,
112 const std::string& endpoint);
113
114 /// @cond
115 // For internal use only.
116 ClientFactory(ClientFactorySettings&& settings,
117 engine::TaskProcessor& channel_task_processor,
118 MiddlewareFactories mws,
119 ugrpc::impl::CompletionQueuePoolBase& queue,
120 ugrpc::impl::StatisticsStorage& statistics_storage,
121 testsuite::GrpcControl& testsuite_grpc,
122 dynamic_config::Source source);
123 /// @endcond
124
125 private:
126 impl::ChannelCache::Token GetChannel(const std::string& client_name,
127 const std::string& endpoint);
128
129 impl::ClientDependencies MakeClientDependencies(ClientSettings&& settings);
130
131 engine::TaskProcessor& channel_task_processor_;
132 MiddlewareFactories mws_;
133 ugrpc::impl::CompletionQueuePoolBase& completion_queues_;
134 impl::ChannelCache channel_cache_;
135 std::unordered_map<std::string, impl::ChannelCache> client_channel_cache_;
136 ugrpc::impl::StatisticsStorage& client_statistics_storage_;
137 const dynamic_config::Source config_source_;
138 testsuite::GrpcControl& testsuite_grpc_;
139};
140
141template <typename Client>
142Client ClientFactory::MakeClient(ClientSettings&& settings) {
143 return Client(MakeClientDependencies(std::move(settings)));
144}
145
146template <typename Client>
147Client ClientFactory::MakeClient(const std::string& client_name,
148 const std::string& endpoint) {
149 ClientSettings settings;
150 settings.client_name = client_name;
151 settings.endpoint = endpoint;
152 return MakeClient<Client>(std::move(settings));
153}
154
155} // namespace ugrpc::client
156
157USERVER_NAMESPACE_END