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.
 
bool IsClientStreaming () const noexcept
 
bool IsServerStreaming () const noexcept
 
const dynamic_config::SnapshotGetInitialDynamicConfig () const
 Get values extracted from dynamic_config. Snapshot will be deleted when the last middleware completes.
 
grpc::ServerContext & GetServerContext ()
 
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::SpanGetSpan ()
 Get the span of the current RPC.
 
utils::AnyStorage< StorageContext > & GetStorageContext ()
 Returns call context for storing per-call custom data.
 

Member Function Documentation

◆ GetServerContext()

grpc::ServerContext & 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
ctx.GetCall().GetStorageContext().Emplace(kAuthUsername, username);
}

In client handler:

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

◆ IsClientStreaming()

bool ugrpc::server::MiddlewareCallContext::IsClientStreaming ( ) const
noexcept
Returns
Is a client-side streaming call

◆ IsServerStreaming()

bool ugrpc::server::MiddlewareCallContext::IsServerStreaming ( ) const
noexcept
Returns
Is a server-side streaming call

◆ 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 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";
return context.SetError(::grpc::Status{::grpc::StatusCode::PERMISSION_DENIED, "Invalid credentials"});
}
}
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: