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