#include <userver/ugrpc/server/middlewares/base.hpp>
|
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::Snapshot & | GetInitialDynamicConfig () 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::Span & | GetSpan () |
| Get the span of the current RPC.
|
|
utils::AnyStorage< StorageContext > & | GetStorageContext () |
| Returns call context for storing per-call custom data.
|
|
◆ GetServerContext()
grpc::ServerContext & ugrpc::server::CallContextBase::GetServerContext |
( |
| ) |
|
|
inherited |
◆ GetStorageContext()
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) {
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:
- MiddlewareBase::OnCallStart - remaining OnCallStart hooks won't be called. Will be called OnCallFinish hooks of middlewares that was called before
SetError
- 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.
- 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 {
auto it = metadata.find(kKey);
if (it == metadata.cend() || it->second != kCredentials) {
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:
- userver/ugrpc/server/middlewares/base.hpp