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