userver: ugrpc::server::MiddlewareCallContext Class Reference
Loading...
Searching...
No Matches
ugrpc::server::MiddlewareCallContext Class Referencefinal

#include <userver/ugrpc/server/middlewares/base.hpp>

Detailed Description

Inheritance diagram for ugrpc::server::MiddlewareCallContext:

Public Member Functions

void SetError (grpc::Status &&status) noexcept
 Aborts the RPC, returning the specified status to the upstream client, see details below.
RpcType GetRpcType () const noexcept
const dynamic_config::Snapshot & GetInitialDynamicConfig () const
 Get values extracted from dynamic_config. Snapshot will be deleted when the last middleware completes.
std::optional< std::size_t > GetLatestRequestSizeBytes () const noexcept
 Serialized size of the request message most recently received on this call.
std::optional< std::size_t > GetLatestResponseSizeBytes () const noexcept
 Serialized size of the response message most recently sent on this call.
grpc::ServerContextBase & GetServerContext ()
const grpc::ServerContextBase & GetServerContext () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
std::string_view GetCallName () const
 Name of the RPC in the format full.path.ServiceName/MethodName.
std::string_view GetServiceName () const
 Get name of gRPC service.
std::string_view GetMethodName () const
 Get name of called gRPC method.
tracing::Span & GetSpan ()
 Get the span of the current RPC.
utils::OptionalRef< google::protobuf::Arena > GetArena () const
 Borrow the call's protobuf arena.
std::shared_ptr< google::protobuf::Arena > GetArenaSharedPtr () const noexcept
 Share ownership of the call's protobuf arena.
utils::AnyStorage< StorageContext > & GetStorageContext ()
 Returns call context for storing per-call custom data.

Member Function Documentation

◆ GetArena()

utils::OptionalRef< google::protobuf::Arena > ugrpc::server::CallContextBase::GetArena ( ) const
inherited

Borrow the call's protobuf arena.

Returns
An empty reference if arenas are disabled or the call is generic. To retain arena messages beyond the call's lifetime, keep a shared pointer obtained with ugrpc::server::CallContextBase::GetArenaSharedPtr.

◆ GetArenaSharedPtr()

std::shared_ptr< google::protobuf::Arena > ugrpc::server::CallContextBase::GetArenaSharedPtr ( ) const
noexceptinherited

Share ownership of the call's protobuf arena.

Returns
An empty pointer if arenas are disabled or the call is generic. The call retains ownership; the returned pointer can extend the lifetime of the arena and its messages beyond the call's lifetime.

◆ GetLatestRequestSizeBytes()

std::optional< std::size_t > ugrpc::server::MiddlewareCallContext::GetLatestRequestSizeBytes ( ) const
noexcept

Serialized size of the request message most recently received on this call.

This is the size of the wire buffer, which is cheaper than calling google::protobuf::Message::ByteSizeLong on the parsed request. For a streaming RPC it is the latest message read so far, not a distinguished final request of the RPC: another request message may still arrive.

While the call is running, only the read side may call this. On a bidirectional stream PostRecvMessage runs concurrently with the write side, and calling this from PreSendMessage or PreSendStatus is a data race. OnCallFinish runs after both sides have completed.

◆ GetLatestResponseSizeBytes()

std::optional< std::size_t > ugrpc::server::MiddlewareCallContext::GetLatestResponseSizeBytes ( ) const
noexcept

Serialized size of the response message most recently sent on this call.

This is the size of the wire buffer, which is cheaper than calling google::protobuf::Message::ByteSizeLong on the response. For a streaming RPC it is the latest message sent so far, not a distinguished final response of the RPC: another response message may still be sent.

The response is serialized after PreSendMessage and PreSendStatus, because those hooks may modify it or replace it with an error. In those hooks the size is empty for unary and client-streaming RPCs, and it is the previous message for a response stream. OnCallFinish runs after serialization; there it is empty if serializing the final response failed.

While the call is running, only the write side may call this. On a bidirectional stream PreSendMessage runs concurrently with the read side, and calling this from PostRecvMessage is a data race. OnCallFinish runs after both sides have completed.

◆ GetRpcType()

RpcType ugrpc::server::MiddlewareCallContext::GetRpcType ( ) const
noexcept
Returns
RPC type

◆ GetServerContext()

grpc::ServerContextBase & ugrpc::server::CallContextBase::GetServerContext ( )
inherited

◆ GetStorageContext()

utils::AnyStorage< StorageContext > & ugrpc::server::CallContextBase::GetStorageContext ( )
inherited

Returns call context for storing per-call custom data.

The context can be used to pass data from server middleware to client handler or from one middleware to another one.

Example usage:

In authentication middleware:

if (password_is_correct) {
// Username is authenticated, set it in per-call storage context
context.GetStorageContext().Emplace(kAuthUsername, username);
}

In client handler:

const auto& username = context.GetStorageContext().Get(kAuthUsername);
auto msg = fmt::format("Hello, {}!", username);

◆ SetError()

void ugrpc::server::MiddlewareCallContext::SetError ( grpc::Status && status)
noexcept

Aborts the RPC, returning the specified status to the upstream client, see details below.

It should be the last command in middlewares hooks.

If that method is called in methods:

  1. MiddlewareBase::OnCallStart - remaining OnCallStart hooks won't be called. Will be called OnCallFinish hooks of middlewares that was called before SetError
  2. MiddlewareBase::PostRecvMessage or ugrpc::server::MiddlewareBase::PreSendMessage :
    • unary: handler won't be called - all. All OnCallFinish hooks will be called.
    • stream: from Read/Write throws a special exception, that ends a handler. All OnCallFinish hooks will be called.
  3. MiddlewareBase::OnCallFinish - all OnCallFinish will be called, despite of SetError and exceptions. If the request is going to end with the error status, then the status is replaced with the status of the current hook.

Example usage

Middleware::Middleware() = default;
void Middleware::OnCallStart(ugrpc::server::MiddlewareCallContext& context) const {
const auto& metadata = context.GetServerContext().client_metadata();
auto it = metadata.find(kKey);
if (it == metadata.cend() || it->second != kCredentials) {
LOG_ERROR() << "Invalid credentials";
context.SetError(::grpc::Status{::grpc::StatusCode::PERMISSION_DENIED, "Invalid credentials"});
return;
}
}
Examples
samples/grpc_middleware_service/src/middlewares/server/auth.cpp, and samples/grpc_middleware_service/src/middlewares/server/meta_filter.cpp.

The documentation for this class was generated from the following file:
  • userver/ugrpc/server/middlewares/base.hpp