7#include <unordered_map>
9#include <grpcpp/channel.h>
10#include <grpcpp/security/credentials.h>
11#include <grpcpp/support/channel_arguments.h>
13#include <userver/concurrent/variable.hpp>
14#include <userver/utils/fixed_array.hpp>
16USERVER_NAMESPACE_BEGIN
18namespace ugrpc::
client::impl {
20class ChannelCache
final {
22 ChannelCache(std::shared_ptr<grpc::ChannelCredentials>&& credentials,
23 const grpc::ChannelArguments& channel_args,
24 std::size_t channel_count);
32 Token Get(
const std::string& endpoint);
35 struct CountedChannel
final {
36 CountedChannel(
const std::string& endpoint,
37 const std::shared_ptr<grpc::ChannelCredentials>& credentials,
38 const grpc::ChannelArguments& channel_args,
41 utils::FixedArray<std::shared_ptr<grpc::Channel>> channels;
42 std::uint64_t counter{0};
45 using Map = std::unordered_map<std::string, CountedChannel>;
47 const std::shared_ptr<grpc::ChannelCredentials> credentials_;
48 const grpc::ChannelArguments channel_args_;
49 const std::size_t channel_count_;
50 concurrent::Variable<Map> channels_;
53class ChannelCache::Token
final {
55 Token()
noexcept =
default;
58 Token(ChannelCache& cache,
const std::string& endpoint,
59 CountedChannel& counted_channel)
noexcept;
61 Token(Token&&)
noexcept;
62 Token& operator=(Token&&)
noexcept;
65 std::size_t GetChannelCount()
const noexcept;
67 const std::shared_ptr<grpc::Channel>& GetChannel(std::size_t index)
const
71 ChannelCache* cache_{
nullptr};
72 const std::string* endpoint_{
nullptr};
73 CountedChannel* counted_channel_{
nullptr};