userver: userver/ugrpc/client/impl/client_data.hpp Source File
Loading...
Searching...
No Matches
client_data.hpp
1#pragma once
2
3#include <cstddef>
4#include <memory>
5#include <utility>
6
7#include <grpcpp/channel.h>
8#include <grpcpp/completion_queue.h>
9
10#include <userver/dynamic_config/source.hpp>
11#include <userver/testsuite/grpc_control.hpp>
12#include <userver/ugrpc/client/impl/channel_cache.hpp>
13#include <userver/ugrpc/client/middlewares/fwd.hpp>
14#include <userver/ugrpc/impl/static_metadata.hpp>
15#include <userver/ugrpc/impl/statistics.hpp>
16#include <userver/utils/fixed_array.hpp>
17#include <userver/utils/rand.hpp>
18
19USERVER_NAMESPACE_BEGIN
20
21namespace ugrpc::client::impl {
22
23struct ClientParams final {
24 std::string client_name;
25 Middlewares mws;
26 grpc::CompletionQueue& queue;
27 ugrpc::impl::ServiceStatistics& statistics_storage;
28 impl::ChannelCache::Token channel_token;
29 const dynamic_config::Source config_source;
30 testsuite::GrpcControl& testsuite_grpc;
31};
32
33/// A helper class for generated gRPC clients
34class ClientData final {
35 public:
36 template <typename Service>
37 using Stub = typename Service::Stub;
38
39 ClientData() = delete;
40
41 template <typename Service>
42 ClientData(ClientParams&& params, ugrpc::impl::StaticServiceMetadata metadata,
43 std::in_place_type_t<Service>)
44 : params_(std::move(params)), metadata_(metadata) {
45 const std::size_t channel_count = GetChannelToken().GetChannelCount();
46 stubs_ = utils::GenerateFixedArray(channel_count, [&](std::size_t index) {
47 return StubPtr(
48 Service::NewStub(GetChannelToken().GetChannel(index)).release(),
49 &StubDeleter<Service>);
50 });
51 }
52
53 ClientData(ClientData&&) noexcept = default;
54 ClientData& operator=(ClientData&&) = delete;
55
56 ClientData(const ClientData&) = delete;
57 ClientData& operator=(const ClientData&) = delete;
58
59 template <typename Service>
60 Stub<Service>& NextStub() const {
61 return *static_cast<Stub<Service>*>(
62 stubs_[utils::RandRange(stubs_.size())].get());
63 }
64
65 grpc::CompletionQueue& GetQueue() const { return params_.queue; }
66
67 dynamic_config::Snapshot GetConfigSnapshot() const {
68 return params_.config_source.GetSnapshot();
69 }
70
71 ugrpc::impl::MethodStatistics& GetStatistics(std::size_t method_id) const {
72 return params_.statistics_storage.GetMethodStatistics(method_id);
73 }
74
75 ChannelCache::Token& GetChannelToken() { return params_.channel_token; }
76
77 std::string_view GetClientName() const { return params_.client_name; }
78
79 const Middlewares& GetMiddlewares() const { return params_.mws; }
80
81 const ugrpc::impl::StaticServiceMetadata& GetMetadata() const {
82 return metadata_;
83 }
84
85 const testsuite::GrpcControl& GetTestsuiteControl() const {
86 return params_.testsuite_grpc;
87 }
88
89 private:
90 using StubDeleterType = void (*)(void*);
91 using StubPtr = std::unique_ptr<void, StubDeleterType>;
92
93 template <typename Service>
94 static void StubDeleter(void* ptr) noexcept {
95 delete static_cast<Stub<Service>*>(ptr);
96 }
97
98 ClientParams params_;
99 ugrpc::impl::StaticServiceMetadata metadata_;
100 utils::FixedArray<StubPtr> stubs_;
101};
102
103template <typename Client>
104ClientData& GetClientData(Client& client) {
105 return client.impl_;
106}
107
108} // namespace ugrpc::client::impl
109
110USERVER_NAMESPACE_END