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