userver: userver/ugrpc/client/simple_client_component.hpp Source File
Loading...
Searching...
No Matches
simple_client_component.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/client/simple_client_component.hpp
4/// @brief @copybrief ugrpc::client::SimpleClientComponent
5
6#include <memory>
7
8#include <userver/components/component_base.hpp>
9#include <userver/utils/not_null.hpp>
10
11#include <userver/ugrpc/client/client_factory.hpp>
12#include <userver/ugrpc/client/fwd.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace ugrpc::client {
17
18namespace impl {
19
20class SimpleClientComponentAny : public components::ComponentBase {
21public:
22 using components::ComponentBase::ComponentBase;
23
24 static yaml_config::Schema GetStaticConfigSchema();
25
26protected:
27 static ClientFactory&
28 FindFactory(const components::ComponentConfig& config, const components::ComponentContext& context);
29
30 static ClientSettings
31 MakeClientSettings(const components::ComponentConfig& config, const dynamic_config::Key<ClientQos>* client_qos);
32};
33
34} // namespace impl
35
36// clang-format off
37
38/// @ingroup userver_components
39///
40/// @brief Template class for a simple gRPC client
41///
42/// The component is used as a storage of a gRPC client if you're OK with
43/// generated client and don't need to wrap it. The client can be fetched using
44/// `GetClient` method.
45///
46/// Example usage:
47///
48/// ```cpp
49/// int main(...)
50/// {
51/// ...
52/// component_list.Append<ugrpc::client::SimpleClientComponent<HelloClient>>("hello-client");
53/// ...
54/// }
55///
56/// MyComponent::MyComponent(const components::ComponentConfig& config,
57/// const components::ComponentContext& context)
58/// {
59/// HelloClient& client = context.FindComponent<
60/// ugrpc::client::SimpleClientComponent<HelloClient>>("hello-client").GetClient();
61/// ... use client ...
62/// }
63/// ```
64///
65/// ## Static config options:
66/// Name | Description | Default value
67/// ---- | ----------- | -------------
68/// endpoint | URL of the gRPC service | --
69/// client-name | name of the gRPC server we talk to, for diagnostics | uses the component name
70/// dedicated-channel-counts | a map of rpc method names to channel counts. Used for high-load methods | -
71/// factory-component | ClientFactoryComponent name to use for client creation | --
72
73// clang-format on
74
75template <typename Client>
76class SimpleClientComponent : public impl::SimpleClientComponentAny {
77public:
78 /// Main component's constructor.
79 SimpleClientComponent(const components::ComponentConfig& config, const components::ComponentContext& context)
80 : SimpleClientComponentAny(config, context),
81 client_(utils::MakeSharedRef<Client>(
82 FindFactory(config, context).MakeClient<Client>(MakeClientSettings(config, nullptr))
83 )) {}
84
85 /// To use a ClientQos config, derive from SimpleClientComponent and provide the parameter at construction:
86 /// @snippet samples/grpc_service/src/greeter_client.hpp component
88 const components::ComponentConfig& config,
89 const components::ComponentContext& context,
90 const dynamic_config::Key<ClientQos>& client_qos
91 )
92 : SimpleClientComponentAny(config, context),
93 client_(utils::MakeSharedRef<Client>(
94 FindFactory(config, context).MakeClient<Client>(MakeClientSettings(config, &client_qos))
95 )) {}
96
97 /// @brief Get gRPC service client.
98 Client& GetClient() noexcept { return *client_; }
99
100protected:
101 /// @brief Get gRPC service client ptr.
102 utils::SharedRef<Client> GetClientPtr() noexcept { return client_; }
103
104private:
105 utils::SharedRef<Client> client_;
106};
107
108} // namespace ugrpc::client
109
110namespace components {
111
112template <typename Client>
113inline constexpr bool kHasValidate<ugrpc::client::SimpleClientComponent<Client>> = true;
114
115} // namespace components
116
117USERVER_NAMESPACE_END