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 <optional>
8#include <vector>
9
10#include <userver/components/loggable_component_base.hpp>
11#include <userver/utils/function_ref.hpp>
12
13#include <userver/ugrpc/client/middlewares/fwd.hpp>
14#include <userver/ugrpc/client/rpc.hpp>
15
16USERVER_NAMESPACE_BEGIN
17
18namespace ugrpc::client {
19
20/// @brief Context for middleware-specific data during gRPC call
21///
22/// It is created for each gRPC Call and it stores aux. data
23/// used by middlewares. Each registered middleware is called by
24/// `Middleware::Handle` with the context passed as an argument.
25/// A middleware may access Call and initial request (if any) using the context.
26class MiddlewareCallContext final {
27 public:
28 /// @cond
29 MiddlewareCallContext(const Middlewares& middlewares, CallAnyBase& call,
30 utils::function_ref<void()> user_call,
31 const ::google::protobuf::Message* request);
32 /// @endcond
33
34 /// @brief Call next plugin, or gRPC handler if none
35 void Next();
36
37 /// @brief Get original gRPC Call
39
40 /// @brief Get initial gRPC request. For RPC w/o initial request
41 /// returns nullptr.
42 const ::google::protobuf::Message* GetInitialRequest();
43
44 private:
45 Middlewares::const_iterator middleware_;
46 Middlewares::const_iterator middleware_end_;
47 std::optional<utils::function_ref<void()>> user_call_;
48
49 CallAnyBase& call_;
50 const ::google::protobuf::Message* request_;
51};
52
53/// @ingroup userver_base_classes
54///
55/// @brief Base class for server gRPC middleware
56class MiddlewareBase {
57 public:
58 MiddlewareBase();
59 MiddlewareBase(const MiddlewareBase&) = delete;
60 MiddlewareBase& operator=(const MiddlewareBase&) = delete;
61 MiddlewareBase& operator=(MiddlewareBase&&) = delete;
62
63 virtual ~MiddlewareBase();
64
65 /// @brief Handles the gRPC request
66 /// @note You MUST call context.Next() inside eventually, otherwise it will
67 /// assert
68 virtual void Handle(MiddlewareCallContext& context) const = 0;
69};
70
71/// @ingroup userver_base_classes
72///
73/// @brief Factory that creates specific client middlewares for clients
75 public:
76 virtual ~MiddlewareFactoryBase();
77
78 virtual std::shared_ptr<const MiddlewareBase> GetMiddleware(
79 std::string_view client_name) const = 0;
80};
81
84
85/// @ingroup userver_base_classes
86///
87/// @brief Base class for client middleware component
89 using components::LoggableComponentBase::LoggableComponentBase;
90
91 public:
92 /// @brief Returns a middleware according to the component's settings
93 virtual std::shared_ptr<const MiddlewareFactoryBase>
95};
96
97} // namespace ugrpc::client
98
99USERVER_NAMESPACE_END