userver: userver/ugrpc/client/client_factory.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
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
8#include <grpcpp/completion_queue.h>
9#include <grpcpp/security/credentials.h>
10#include <grpcpp/support/channel_arguments.h>
11
12#include <userver/dynamic_config/source.hpp>
13#include <userver/engine/task/task_processor_fwd.hpp>
14#include <userver/logging/level.hpp>
15#include <userver/testsuite/grpc_control.hpp>
16#include <userver/utils/statistics/fwd.hpp>
17#include <userver/yaml_config/fwd.hpp>
18
19#include <userver/ugrpc/client/impl/channel_cache.hpp>
20#include <userver/ugrpc/client/impl/client_data.hpp>
21#include <userver/ugrpc/client/middlewares/base.hpp>
22#include <userver/ugrpc/impl/statistics_storage.hpp>
23
24USERVER_NAMESPACE_BEGIN
25
26namespace ugrpc::client {
27
28/// Settings relating to the ClientFactory
30 /// gRPC channel credentials, none by default
31 std::shared_ptr<grpc::ChannelCredentials> credentials{
33
34 /// Optional grpc-core channel args
35 /// @see https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
36 grpc::ChannelArguments channel_args{};
37
38 /// The logging level override for the internal grpcpp library. Must be either
39 /// `kDebug`, `kInfo` or `kError`.
41
42 /// Number of underlying channels that will be created for every client
43 /// in this factory.
44 std::size_t channel_count{1};
45};
46
48 formats::parse::To<ClientFactoryConfig>);
49
50/// @brief Creates generated gRPC clients. Has a minimal built-in channel cache:
51/// as long as a channel to the same endpoint is used somewhere, the same
52/// channel is given out.
53class ClientFactory final {
54 public:
55 ClientFactory(ClientFactoryConfig&& config,
56 engine::TaskProcessor& channel_task_processor,
57 MiddlewareFactories mws, grpc::CompletionQueue& queue,
58 utils::statistics::Storage& statistics_storage,
59 testsuite::GrpcControl& testsuite_grpc,
60 dynamic_config::Source source);
61
62 template <typename Client>
63 Client MakeClient(const std::string& client_name,
64 const std::string& endpoint);
65
66 private:
67 impl::ChannelCache::Token GetChannel(const std::string& endpoint);
68
69 engine::TaskProcessor& channel_task_processor_;
70 MiddlewareFactories mws_;
71 grpc::CompletionQueue& queue_;
72 impl::ChannelCache channel_cache_;
73 ugrpc::impl::StatisticsStorage client_statistics_storage_;
74 const dynamic_config::Source config_source_;
75 testsuite::GrpcControl& testsuite_grpc_;
76};
77
78template <typename Client>
79Client ClientFactory::MakeClient(const std::string& client_name,
80 const std::string& endpoint) {
81 auto& statistics =
82 client_statistics_storage_.GetServiceStatistics(Client::GetMetadata());
83
84 Middlewares mws;
85 mws.reserve(mws_.size());
86 for (const auto& mw_factory : mws_)
87 mws.push_back(mw_factory->GetMiddleware(client_name));
88
89 return Client(impl::ClientParams{client_name, std::move(mws), queue_,
90 statistics, GetChannel(endpoint),
91 config_source_, testsuite_grpc_});
92}
93
94} // namespace ugrpc::client
95
96USERVER_NAMESPACE_END