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/client_factory_settings.hpp>
23#include <userver/ugrpc/client/fwd.hpp>
24#include <userver/ugrpc/client/impl/channel_cache.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 creation of a code-generated client
37struct ClientSettings final {
38 /// **(Required)**
39 /// The name of the client, for diagnostics, credentials and middlewares.
40 std::string client_name;
41
42 /// **(Required)**
43 /// The URI to connect to, e.g. `http://my.domain.com:8080`.
44 /// Should not include any HTTP path, just schema, domain name and port. Unix
45 /// sockets are also supported. For details, see:
46 /// https://grpc.github.io/grpc/cpp/md_doc_naming.html
47 std::string endpoint;
48
49 /// **(Optional)**
50 /// The name of the QOS
51 /// @ref scripts/docs/en/userver/dynamic_config.md "dynamic config"
52 /// that will be applied automatically to every RPC.
53 ///
54 /// Timeout from QOS config is ignored if:
55 ///
56 /// * an explicit `qos` parameter is specified at RPC creation, or
57 /// * deadline is specified in the `client_context` passed at RPC creation.
58 ///
59 /// ## Client QOS config definition sample
60 ///
61 /// @snippet grpc/tests/tests/unit_test_client_qos.hpp qos config key
62 const dynamic_config::Key<ClientQos>* client_qos{nullptr};
63
64 /// **(Optional)**
65 /// Dedicated high-load methods that have separate channels
67};
68
69/// @ingroup userver_clients
70///
71/// @brief Creates gRPC clients.
72///
73/// Typically obtained from ugrpc::client::ClientFactoryComponent.
74/// In tests and benchmarks, obtained from ugrpc::tests::ServiceBase and
75/// friends.
76///
77/// Has a minimal built-in channel cache:
78/// as long as a channel to the same endpoint is used somewhere, the same
79/// channel is given out.
80class ClientFactory final {
81public:
82 /// @brief Make a client of the specified code-generated type.
83 template <typename Client>
84 Client MakeClient(ClientSettings&& settings);
85
86 /// @deprecated Use the overload taking @ref ClientSettings instead.
87 /// @brief Make a client of the specified code-generated type.
88 /// @param client_name see @ref ClientSettings
89 /// @param endpoint see @ref ClientSettings
90 template <typename Client>
91 Client MakeClient(const std::string& client_name, const std::string& endpoint);
92
93 /// @cond
94 // For internal use only.
95 ClientFactory(
96 ClientFactorySettings&& settings,
97 engine::TaskProcessor& channel_task_processor,
98 MiddlewareFactories mws,
99 ugrpc::impl::CompletionQueuePoolBase& queue,
100 ugrpc::impl::StatisticsStorage& statistics_storage,
101 testsuite::GrpcControl& testsuite_grpc,
102 dynamic_config::Source source
103 );
104 /// @endcond
105
106private:
107 impl::ChannelCache::Token GetChannel(const std::string& client_name, const std::string& endpoint);
108
109 impl::ClientDependencies MakeClientDependencies(ClientSettings&& settings);
110
111 ClientFactorySettings settings_;
112 engine::TaskProcessor& channel_task_processor_;
113 MiddlewareFactories mws_;
114 ugrpc::impl::CompletionQueuePoolBase& completion_queues_;
115 impl::ChannelCache channel_cache_;
116 std::unordered_map<std::string, impl::ChannelCache> client_channel_cache_;
117 ugrpc::impl::StatisticsStorage& client_statistics_storage_;
118 const dynamic_config::Source config_source_;
119 testsuite::GrpcControl& testsuite_grpc_;
120};
121
122template <typename Client>
123Client ClientFactory::MakeClient(ClientSettings&& settings) {
124 return Client(MakeClientDependencies(std::move(settings)));
125}
126
127template <typename Client>
128Client ClientFactory::MakeClient(const std::string& client_name, const std::string& endpoint) {
129 ClientSettings settings;
130 settings.client_name = client_name;
131 settings.endpoint = endpoint;
132 return MakeClient<Client>(std::move(settings));
133}
134
135} // namespace ugrpc::client
136
137USERVER_NAMESPACE_END