userver
C++ Async Framework
Loading...
Searching...
No Matches
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
/
ugrpc
/
server
/
storage_context
.
hpp
>
10
#
include
<
userver
/
utils
/
any_storage
.
hpp
>
11
12
USERVER_NAMESPACE_BEGIN
13
14
namespace
ugrpc::
server
{
15
16
class
CallAnyBase
;
17
18
/// @brief gRPC call context
19
class
CallContext
{
20
public
:
21
/// @cond
22
explicit
CallContext(
CallAnyBase
& call);
23
/// @endcond
24
25
/// @returns the `ServerContext` used for this RPC
26
grpc::ServerContext&
GetServerContext
();
27
28
/// @brief Name of the RPC in the format `full.path.ServiceName/MethodName`
29
std
::
string_view
GetCallName
()
const
;
30
31
/// @brief Get name of gRPC service
32
std
::
string_view
GetServiceName
()
const
;
33
34
/// @brief Get name of called gRPC method
35
std
::
string_view
GetMethodName
()
const
;
36
37
/// @brief Get the span of the current RPC
38
tracing
::Span&
GetSpan
();
39
40
/// @brief Returns call context for storing per-call custom data
41
///
42
/// The context can be used to pass data from server middleware to client
43
/// handler or from one middleware to another one.
44
///
45
/// ## Example usage:
46
///
47
/// In authentication middleware:
48
///
49
/// @code
50
/// if (password_is_correct) {
51
/// // Username is authenticated, set it in per-call storage context
52
/// ctx.GetCall().GetStorageContext().Emplace(kAuthUsername, username);
53
/// }
54
/// @endcode
55
///
56
/// In client handler:
57
///
58
/// @code
59
/// const auto& username = context.GetStorageContext().Get(kAuthUsername);
60
/// auto msg = fmt::format("Hello, {}!", username);
61
/// @endcode
62
utils
::AnyStorage<
StorageContext
>&
GetStorageContext
();
63
64
protected
:
65
/// @cond
66
const
CallAnyBase
& GetCall()
const
;
67
68
CallAnyBase
& GetCall();
69
/// @endcond
70
71
private
:
72
CallAnyBase
& call_;
73
};
74
75
/// @brief generic gRPC call context
76
class
GenericCallContext
:
public
CallContext
{
77
public
:
78
/// @cond
79
using
CallContext
::CallContext;
80
/// @endcond
81
82
/// @brief Set a custom call name for metric labels
83
void
SetMetricsCallName
(std::string_view call_name);
84
};
85
86
}
// namespace ugrpc::server
87
88
USERVER_NAMESPACE_END
userver
ugrpc
server
call_context.hpp
Generated on Wed Dec 4 2024 18:42:29 for userver by
Doxygen
1.10.0