userver: userver/ugrpc/client/middlewares/base.hpp Source File
Loading...
Searching...
No Matches
base.hpp
1#pragma once
2
3/// @file userver/ugrpc/client/middlewares/middleware_base.hpp
4/// @brief @copybrief ugrpc::client::MiddlewareBase
5
6#include <memory>
7#include <vector>
8
9#include <userver/components/component_base.hpp>
10
11#include <userver/ugrpc/client/middlewares/fwd.hpp>
12#include <userver/ugrpc/client/rpc.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace ugrpc::client {
17
18/// @brief Context for middleware-specific data during gRPC call
19///
20/// It is created for each gRPC Call and it stores aux. data
21/// used by middlewares. Each registered middleware is called by
22/// `Middleware::Handle` with the context passed as an argument.
23/// A middleware may access Call and initial request (if any) using the context.
24class MiddlewareCallContext final {
25public:
26 /// @cond
27 explicit MiddlewareCallContext(impl::RpcData& data);
28 /// @endcond
29
30 /// @returns the `ClientContext` used for this RPC
31 grpc::ClientContext& GetContext() noexcept;
32
33 /// @returns client name
34 std::string_view GetClientName() const noexcept;
35
36 /// @returns RPC name
37 std::string_view GetCallName() const noexcept;
38
39 /// @returns RPC kind
40 CallKind GetCallKind() const noexcept;
41
42 /// @returns RPC span
43 tracing::Span& GetSpan() noexcept;
44
45 /// @cond
46 // For internal use only
47 impl::RpcData& GetData(ugrpc::impl::InternalTag);
48 /// @endcond
49
50private:
51 impl::RpcData& data_;
52};
53
54/// @ingroup userver_base_classes
55///
56/// @brief Base class for client gRPC middleware
57class MiddlewareBase {
58public:
59 virtual ~MiddlewareBase();
60
61 MiddlewareBase(const MiddlewareBase&) = delete;
62 MiddlewareBase(MiddlewareBase&&) = delete;
63
64 MiddlewareBase& operator=(const MiddlewareBase&) = delete;
65 MiddlewareBase& operator=(MiddlewareBase&&) = delete;
66
67 /// @brief This function is called before rpc, on each rpc. It does nothing by
68 /// default
69 virtual void PreStartCall(MiddlewareCallContext&) const;
70
71 /// @brief This function is called before sending message, on each request. It
72 /// does nothing by default
73 /// @note Not called for `GenericClient` messages
74 virtual void PreSendMessage(MiddlewareCallContext&, const google::protobuf::Message&) const;
75
76 /// @brief This function is called after receiving message, on each response.
77 /// It does nothing by default
78 /// @note Not called for `GenericClient` messages
79 virtual void PostRecvMessage(MiddlewareCallContext&, const google::protobuf::Message&) const;
80
81 /// @brief This function is called after rpc, on each rpc. It does nothing by
82 /// default
83 /// @note Could be not called in case of deadline or network problem
84 /// @see @ref RpcInterruptedError
85 virtual void PostFinish(MiddlewareCallContext&, const grpc::Status&) const;
86
87protected:
88 MiddlewareBase();
89};
90
91/// @ingroup userver_base_classes
92///
93/// @brief Factory that creates specific client middlewares for clients
95public:
96 virtual ~MiddlewareFactoryBase();
97
98 virtual std::shared_ptr<const MiddlewareBase> GetMiddleware(std::string_view client_name) const = 0;
99};
100
102
103/// @ingroup userver_base_classes
104///
105/// @brief Base class for client middleware component
107 using components::ComponentBase::ComponentBase;
108
109public:
110 /// @brief Returns a middleware according to the component's settings
111 virtual std::shared_ptr<const MiddlewareFactoryBase> GetMiddlewareFactory() = 0;
112};
113
114namespace impl {
115
116Middlewares InstantiateMiddlewares(const MiddlewareFactories& factories, const std::string& client_name);
117
118} // namespace impl
119
120} // namespace ugrpc::client
121
122USERVER_NAMESPACE_END