userver: userver/ugrpc/client/generic_client.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
generic_client.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/client/generic_client.hpp
4/// @brief @copybrief ugrpc::client::GenericClient
5
6#include <optional>
7#include <string_view>
8
9#include <grpcpp/client_context.h>
10#include <grpcpp/support/byte_buffer.h>
11
12#include <userver/ugrpc/client/impl/client_data.hpp>
13#include <userver/ugrpc/client/qos.hpp>
14#include <userver/ugrpc/client/response_future.hpp>
15#include <userver/ugrpc/impl/static_service_metadata.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace ugrpc::client {
20
22 /// Client QOS for this call. Note that there is no QOS dynamic config by
23 /// default, so unless a timeout is specified here, only the deadline
24 /// propagation mechanism will affect the gRPC deadline.
25 Qos qos{};
26
27 /// If non-`nullopt`, metrics are accounted for specified fake call name.
28 /// If `nullopt`, writes a set of metrics per real call name.
29 /// If the microservice serves as a proxy and has untrusted clients, it is
30 /// a good idea to have this option set to non-`nullopt` to avoid
31 /// the situations where an upstream client can spam various RPCs with
32 /// non-existent names, which leads to this microservice spamming RPCs
33 /// with non-existent names, which leads to creating storage for infinite
34 /// metrics and causes OOM.
35 /// The default is to specify `"Generic/Generic"` fake call name.
36 std::optional<std::string_view> metrics_call_name{"Generic/Generic"};
37};
38
39/// @ingroup userver_clients
40///
41/// @brief Allows to talk to gRPC services (generic and normal) using dynamic
42/// method names.
43///
44/// Created using @ref ugrpc::client::ClientFactory::MakeClient.
45///
46/// `call_name` must be in the format `full.path.to.TheService/MethodName`.
47/// Note that unlike in base grpc++, there must be no initial `/` character.
48///
49/// The API is mainly intended for proxies, where the request-response body is
50/// passed unchanged, with settings taken solely from the RPC metadata.
51/// In cases where the code needs to operate on the actual messages,
52/// serialization of requests and responses is left as an exercise to the user.
53///
54/// Middlewares are customizable and are applied as usual, except that no
55/// message hooks are called, meaning that there won't be any logs of messages
56/// from the default middleware.
57///
58/// There are no per-call-name metrics by default,
59/// for details see @ref GenericOptions::metrics_call_name.
60///
61/// ## Example GenericClient usage with known message types
62///
63/// @snippet grpc/tests/generic_client_test.cpp sample
64///
65/// For a more complete sample, see @ref grpc_generic_api.
66class GenericClient final {
67public:
68 GenericClient(GenericClient&&) noexcept = default;
69 GenericClient& operator=(GenericClient&&) noexcept = delete;
70
71 /// Initiate a `single request -> single response` RPC with the given name.
72 client::ResponseFuture<grpc::ByteBuffer> AsyncUnaryCall(
73 std::string_view call_name,
74 const grpc::ByteBuffer& request,
75 std::unique_ptr<grpc::ClientContext> context = std::make_unique<grpc::ClientContext>(),
76 const GenericOptions& options = {}
77 ) const;
78
79 /// Initiate a `single request -> single response` RPC with the given name.
80 grpc::ByteBuffer UnaryCall(
81 std::string_view call_name,
82 const grpc::ByteBuffer& request,
83 std::unique_ptr<grpc::ClientContext> context = std::make_unique<grpc::ClientContext>(),
84 const GenericOptions& options = {}
85 ) const;
86
87 /// @cond
88 // For internal use only.
89 explicit GenericClient(impl::ClientInternals&&);
90
91 static std::optional<ugrpc::impl::StaticServiceMetadata> GetMetadata() { return std::nullopt; }
92 /// @endcond
93
94private:
95 template <typename Client>
96 friend impl::ClientData& impl::GetClientData(Client& client);
97
98 impl::ClientData impl_;
99};
100
101} // namespace ugrpc::client
102
103USERVER_NAMESPACE_END