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_fwd.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace ugrpc::server {
17
18namespace impl {
19class CallAnyBase;
20} // namespace impl
21
22class CallContextBase {
23public:
24 /// @cond
25 /// For internal use only
26 CallContextBase(utils::impl::InternalTag, impl::CallAnyBase& call);
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 const impl::CallAnyBase& GetCall(utils::impl::InternalTag) const;
74
75 impl::CallAnyBase& GetCall(utils::impl::InternalTag);
76
77 // Prevent destruction via pointer to base.
78 ~CallContextBase() = default;
79
80 /// @endcond
81
82private:
83 impl::CallAnyBase& call_;
84};
85
86/// @brief gRPC call context
87class CallContext final : public CallContextBase {
88public:
89 /// @cond
90 using CallContextBase::CallContextBase;
91 /// @endcond
92};
93
94/// @brief generic gRPC call context
95class GenericCallContext final : public CallContextBase {
96public:
97 /// @cond
98 using CallContextBase::CallContextBase;
99 /// @endcond
100
101 /// @brief Set a custom call name for metric labels
102 void SetMetricsCallName(std::string_view call_name);
103};
104
105} // namespace ugrpc::server
106
107USERVER_NAMESPACE_END