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 rpc method names to channel counts. 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, derive from SimpleClientComponent and provide the parameter at construction:
81 /// @snippet samples/grpc_service/src/greeter_client.hpp component
83 const components::ComponentConfig& config,
84 const components::ComponentContext& context,
85 const dynamic_config::Key<ClientQos>& client_qos
86 )
87 : SimpleClientComponentAny(config, context),
88 client_(FindFactory(config, context).MakeClient<Client>(MakeClientSettings(config, &client_qos))) {}
89
90 /// @brief Get gRPC service client
91 Client& GetClient() { return client_; }
92
93private:
94 Client client_;
95};
96
97} // namespace ugrpc::client
98
99namespace components {
100
101template <typename Client>
102inline constexpr bool kHasValidate<ugrpc::client::SimpleClientComponent<Client>> = true;
103
104} // namespace components
105
106USERVER_NAMESPACE_END