userver: userver/ugrpc/server/call_context.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
call_context.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/server/call_context.hpp
4/// @brief @copybrief ugrpc::server::CallContext
5
6#include <grpcpp/server_context.h>
7
8#include <userver/tracing/span.hpp>
9#include <userver/utils/any_storage.hpp>
10
11#include <userver/ugrpc/server/storage_context.hpp>
12#include <userver/utils/impl/internal_tag.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace ugrpc::server {
17
18namespace impl {
19struct CallState;
20} // namespace impl
21
22class CallContextBase {
23public:
24 /// @cond
25 // For internal use only.
26 CallContextBase(utils::impl::InternalTag, impl::CallState& state);
27 /// @endcond
28
29 CallContextBase(CallContextBase&&) = delete;
30 CallContextBase& operator=(CallContextBase&&) = delete;
31
32 /// @returns the `ServerContext` used for this RPC
33 grpc::ServerContext& GetServerContext();
34
35 /// @brief Name of the RPC in the format `full.path.ServiceName/MethodName`
36 std::string_view GetCallName() const;
37
38 /// @brief Get name of gRPC service
39 std::string_view GetServiceName() const;
40
41 /// @brief Get name of called gRPC method
42 std::string_view GetMethodName() const;
43
44 /// @brief Get the span of the current RPC
45 tracing::Span& GetSpan();
46
47 /// @brief Returns call context for storing per-call custom data
48 ///
49 /// The context can be used to pass data from server middleware to client
50 /// handler or from one middleware to another one.
51 ///
52 /// ## Example usage:
53 ///
54 /// In authentication middleware:
55 ///
56 /// @code
57 /// if (password_is_correct) {
58 /// // Username is authenticated, set it in per-call storage context
59 /// ctx.GetCall().GetStorageContext().Emplace(kAuthUsername, username);
60 /// }
61 /// @endcode
62 ///
63 /// In client handler:
64 ///
65 /// @code
66 /// const auto& username = context.GetStorageContext().Get(kAuthUsername);
67 /// auto msg = fmt::format("Hello, {}!", username);
68 /// @endcode
69 utils::AnyStorage<StorageContext>& GetStorageContext();
70
71protected:
72 /// @cond
73 // For internal use only.
74 const impl::CallState& GetCallState(utils::impl::InternalTag) const { return state_; }
75
76 // For internal use only.
77 impl::CallState& GetCallState(utils::impl::InternalTag) { return state_; }
78
79 // Prevent destruction via pointer to base.
80 ~CallContextBase() = default;
81 /// @endcond
82
83private:
84 impl::CallState& state_;
85};
86
87/// @brief gRPC call context
88class CallContext final : public CallContextBase {
89public:
90 /// @cond
91 using CallContextBase::CallContextBase;
92 /// @endcond
93};
94
95/// @brief generic gRPC call context
96class GenericCallContext final : public CallContextBase {
97public:
98 /// @cond
99 using CallContextBase::CallContextBase;
100 /// @endcond
101
102 /// @brief Set a custom call name for metric labels
103 void SetMetricsCallName(std::string_view call_name);
104};
105
106} // namespace ugrpc::server
107
108USERVER_NAMESPACE_END