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 <string>
7#include <utility>
8
9#include <userver/dynamic_config/source.hpp>
10#include <userver/engine/task/task_processor_fwd.hpp>
11#include <userver/testsuite/grpc_control.hpp>
12
13#include <userver/ugrpc/client/client_factory_settings.hpp>
14#include <userver/ugrpc/client/client_settings.hpp>
15#include <userver/ugrpc/client/impl/client_dependencies.hpp>
16#include <userver/ugrpc/client/middlewares/base.hpp>
17
18USERVER_NAMESPACE_BEGIN
19
20namespace ugrpc::impl {
21class StatisticsStorage;
22class CompletionQueuePoolBase;
23} // namespace ugrpc::impl
24
25namespace ugrpc::client {
26
27/// @ingroup userver_clients
28///
29/// @brief Creates gRPC clients.
30///
31/// Typically obtained from ugrpc::client::ClientFactoryComponent.
32/// In tests and benchmarks, obtained from ugrpc::tests::ServiceBase and
33/// friends.
34///
35/// Has a minimal built-in channel cache:
36/// as long as a channel to the same endpoint is used somewhere, the same
37/// channel is given out.
38class ClientFactory final {
39public:
40 /// @brief Make a client of the specified code-generated type.
41 template <typename Client>
42 Client MakeClient(ClientSettings&& client_settings);
43
44 /// @deprecated Use the overload taking @ref ClientSettings instead.
45 /// @brief Make a client of the specified code-generated type.
46 /// @param client_name see @ref ClientSettings
47 /// @param endpoint see @ref ClientSettings
48 template <typename Client>
49 Client MakeClient(const std::string& client_name, const std::string& endpoint);
50
51 /// @cond
52 // For internal use only.
53 ClientFactory(
54 ClientFactorySettings&& client_factory_settings,
55 engine::TaskProcessor& channel_task_processor,
56 MiddlewareFactories mws,
57 ugrpc::impl::CompletionQueuePoolBase& completion_queues,
58 ugrpc::impl::StatisticsStorage& statistics_storage,
59 testsuite::GrpcControl& testsuite_grpc,
60 dynamic_config::Source config_source
61 );
62 /// @endcond
63
64private:
65 impl::ClientDependencies MakeClientDependencies(ClientSettings&& settings);
66
67 ClientFactorySettings client_factory_settings_;
68 engine::TaskProcessor& channel_task_processor_;
69 MiddlewareFactories mws_;
70 ugrpc::impl::CompletionQueuePoolBase& completion_queues_;
71 ugrpc::impl::StatisticsStorage& client_statistics_storage_;
72 const dynamic_config::Source config_source_;
73 testsuite::GrpcControl& testsuite_grpc_;
74};
75
76template <typename Client>
77Client ClientFactory::MakeClient(ClientSettings&& settings) {
78 return Client(MakeClientDependencies(std::move(settings)));
79}
80
81template <typename Client>
82Client ClientFactory::MakeClient(const std::string& client_name, const std::string& endpoint) {
83 ClientSettings settings;
84 settings.client_name = client_name;
85 settings.endpoint = endpoint;
86 return MakeClient<Client>(std::move(settings));
87}
88
89} // namespace ugrpc::client
90
91USERVER_NAMESPACE_END